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.
Pre-Requisites
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
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. In case of Java, the setCacheSyncDependencyproperty is used to add multi-cache key dependency to the item.
try
{
// Connect to the caches for cachesync dependency
ICache cache1 = CacheManager.GetCache("dataCache");
ICache cache2 = 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:
// cache.Contains()
// cache.Count
}
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
}
Recommendation: 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.
See Also
Key Dependency Types and Usage
Aggregate Dependency
Data Expiration
Cache Data Dependency on External Source