• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Multi Cache Sync Dependency Usage

Note

This feature is only available in NCache Enterprise Edition.

Cache synchronization dependency (also known as multi-cache key dependency) is provided by NCache to synchronize two separate caches so that an item updated or removed from one cache can have the same effect on the synchronized cache. Multi-Cache key dependency synchronizes two independent caches using Item Level notifications provided by NCache. Whenever an item is updated or removed from one cache, a key based notification is fired to the other to synchronize data in both caches.

CacheSyncDependency synchronizes two caches using Item Level notifications provided by NCache. Whenever an item is updated or removed from one cache, a key based notification is fired to the other to remove or update data in both caches.

When to Use Multi-Cache Key Dependency

Multiple clients use multiple caches for same application. An online store uses two caches for keeping the data of the data store, One cache stores the session data and the other cache stores the product data along with all the information.

Consider a scenario where a new customer logs in and adds a new product to its bucket list. During that another script executes that updates the product information i.e. unit price, available stocks and a flag that shows the availability of the product which discontinues the data of that product that is to be added whereas this data is already present in the user’s bucket in the other cache. This will give an information conflict in between the data. So on checking out, the customer gets the error that the product is not available in the stock. Cache Sync Dependency helps in this scenario. On applying dependency on the product item in this case it will update the session data about the update in the product information due to which the data will remain synced between both the caches.

NCache cache sync dependency provides a way to synchronize two caches, so that an item updated or removed from one cache have the same effect on the synchronized cache.

Prerequisites

  • .NET/.NET Core
  • Java
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: ICache, CacheItem, CacheSyncDependency, GetCache.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: Cache, CacheItem, setSyncDependency, getCache.

Add Data in Cache with Multi-Cache Key Dependency

The following code explains the use of cache sync dependency. In this example an item in the clustered cache 'sessionCache' is replicated in the cache 'dataCache' with CacheSyncDependency. Any change in the clustered cache will automatically be updated in the dependent item in the local cache.

  • .NET/.NET Core
  • Java
try
{
      // Connect to the caches for cachesync dependency
      ICache dataCache = CacheManager.GetCache("dataCache");
      ICache sessionCache = CacheManager.GetCache("sessionCache");

      // Get product from database against given ProductID
      Product product = FetchProductFromDB(1001);

      string key = $"Product:{product.ProductID}";

      // Insert the item in cache1
      cache1.Insert(key, product);

      //Set the cachesync dependency for this key
      var dependency = new CacheSyncDependency("dataCache", key);

      var cacheItem = new CacheItem(key);
      cacheItem.SyncDependency = dependency;

      // Insert the item with the same key in cache2
      cache2.Insert(key, cacheItem);

      // For successful addition of item verify using:
      // PerfMon counters or API
}
catch (OperationFailedException ex)
{
      // NCache specific exception
      if (ex.ErrorCode == NCacheErrorCodes.NO_SERVER_AVAILABLE)
      {
            // Make sure NCache Service is running
            // Make sure that the cache is running
      }
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (ConfigurationException ex)
{
      if(ex.ErrorCode == NCacheErrorCodes.SERVER_INFO_NOT_FOUND)
      {
            // client.ncconf must have server information
      }
}
catch (Exception ex)
{
      // Any generic exception like ArgumentNullException or ArgumentException
      // Argument exception occurs in case of empty string name
}
try
{
      // Connect to the caches for cachesync dependency
      Cache cache1 = CacheManager.getCache("dataCache");
      Cache cache2 = CacheManager.getCache("sessionCache");

      // Get product from database against productId
      Product product = new Product(productId);

      // Create unique key for this product
      String key = "Product:" + product.productId;

      // Insert product in cache1
      cache1.insert(key, product);

      // Set cachesync dependency for this key
      var dependency = new CacheSyncDependency("dataCache", key);

      // Create new cache item and set cache sync dependency to it
      CacheItem cacheItem = new CacheItem(product);
      cacheItem.setSyncDependency(dependency);

      // Insert cache item with the same key in cache2
      cache2.insert(key, cacheItem);

      // For successful insertion of item with cache sync dependency
      // verify using:
      // boolean keyPresent = cache.contains(key);
      // long cacheSize = cache.getCount();
}
catch (OperationFailedException ex)
{
      // NCache specific exception
      if (ex.getErrorCode() == NCacheErrorCodes.NO_SERVER_AVAILABLE)
      {
            // Make sure NCache Service is running
            // Make sure that the cache is running
      }
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (ConfigurationException ex)
{
      if (ex.getErrorCode() == NCacheErrorCodes.SERVER_INFO_NOT_FOUND)
      {
            // client.ncconf must have server information
      }
}
catch (Exception ex)
{
    // Any generic exception like IllegalArgumentException or NullPointerException
}
Note

To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

Important

For cache synchronization dependency, an item must exist in cache before another item can be added with a dependency on it.

Additional Resources

NCache provides sample application for Key Dependency on GitHub.

See Also

Key Dependency Types and Usage
Aggregate Dependency
Data Expiration
Cache Data Dependency on External Source

Back to top Copyright © 2017 Alachisoft