Multi-Cache Sync Dependency Usage
Note
This feature is only available in NCache Enterprise.
NCache provides the cache synchronization dependency (also known as a multi-cache key dependency) to synchronize two separate caches so that an item updated or removed from one 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.
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 the same application. An online store uses two caches for keeping the data store data. One cache stores the session data, and the other stores the product data and all the information.
Consider a scenario where a new customer logs in and adds a new product to their bucket list. During that, another script executes that updates the product information, i.e., unit price, available stocks, and a flag that shows product availability, which discontinues the data of that product to be added. This data is already available in the user’s bucket in the other cache, causing an information conflict between the data. So on checking out, the customer gets an error stating that the product is out of stock. Cache Sync Dependency helps in this scenario. Upon employing the dependency, it will update the session data about the update in the product information, and the data remains synced between both caches.
NCache cache sync dependency provides a way to synchronize two caches so that an item updated or removed from one cache has the same effect on the synchronized cache.
Prerequisites
- 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.
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 demoCache is replicated in the cache demoClusteredCache with CacheSyncDependency
. Any change in the clustered cache will automatically update the dependent item in the local cache.
// Connect to the caches for cachesync dependency
ICache dataCache = CacheManager.GetCache("demoCache");
ICache sessionCache = CacheManager.GetCache("demoClusteredCache");
// Get product from database against given ProductID
Product product = FetchProductFromDB(1001);
string key = $"Product:{product.ProductID}";
// Insert the item in demoCache
dataCache.Insert(key, product);
//Set the cachesync dependency for this key
var dependency = new CacheSyncDependency("demoCache", key);
var cacheItem = new CacheItem(key);
cacheItem.SyncDependency = dependency;
// Insert the item with the same key in demoCache2
sessionCache.Insert(key, cacheItem);
// For successful addition of item verify using:
// PerfMon counters or API
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 the cache before another item can be added with a dependency on it.
Additional Resources
NCache provides a sample application for Key Dependency on GitHub.
See Also
Key Dependency Types and Usage
Aggregate Dependency
Data Expiration
Cache Data Dependency on External Source