Multi-Cache Sync Dependency Usage
NCache provides the CacheSyncDependency
(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.
The 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 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. The CacheSyncDependency
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 synchronized between both caches.
NCache CacheSyncDependency
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 CacheSyncDependency
. 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 CacheSyncDependency
, 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
.NET: Alachisoft.NCache.Runtime.Dependencies namespace.
Java: com.alachisoft.ncache.runtime.dependencies namespace.