Multi-Cache Key Dependency
Cache synchronization 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. Cache sync 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 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 for Using Multi-Cache Key Dependency
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Dependencies
- 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.
try
{
// Initialize the caches for cachesync dependency
Cache cache1 = NCache.InitializeCache("dataCache");
Cache cache2 = NCache.InitializeCache("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);
// For successful addition of item verify using:
// cache.Contains()
// cache.Count
//Set the cachesync dependency for this key
CacheSyncDependency dependency = new CacheSyncDependency("sessionCache", key);
CacheItem 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.Message.Contains("No server is available to process the request for sessionCache"))
{
// If one of the cache is not started
// Cache name will be the name of your own cache
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
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
Aggregate Dependency
Data Expiration
Sync Cache with External Source