Multiple Cache Sync Dependency
Cache sync 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. For example, you can have a local cache that keeps items that are frequently used by your application and a clustered cache that keeps a larger number of items from being shared with other applications. Moreover, in your local cache, items are kept synchronized with the clustered cache so you never have any data integrity problems while improving your application’s performance even further.
Cache sync dependency 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 synchronize data in both 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.
The following code explains the use of cache sync dependency. In this example an item in the clustered cache 'myreplicatedcache' is replicated in the local cache 'mycache' with cache sync dependency. Any change in the clustered cache will automatically be updated in the dependent item in the local cache.> For cache sync dependency, an item must exist in cache before another item can be added with a dependency on it.
To utilize the API, include the following namespace in your application:
Alachisoft.NCache.Runtime.Dependencies.
try
{
Cache cache1 = NCache.InitializeCache("mycache");
Cache cache2 = NCache.InitializeCache("myreplicatedcache");
Product product = new Product();
product.ProductID = 1001;
product.ProductName = "Chai";
string key = "Product:" + product.ProductID;
cache1.Insert(key, product);
CacheSyncDependency dependency = new CacheSyncDependency("mycache", key);
CacheItem cacheItem = new CacheItem(cache1.Get(key));
cacheItem.SyncDependency = dependency;
cache2.Insert(key, cacheItem);
//Remove/Delete item from cache1
cache1.Remove(key);
//... and then check for its existence in cache2
object item = cache2.Get(key);
if (item == null){
// item removed successfully
}
else{
// item not removed successfully
}
}
catch (OperationFailedException e){
// handle exception if any cache operation fails
}