NCache 4.4 - Online Documentation

Synchronize Data across Multiple 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.
 
Adding Data with Cache Sync Dependency
 
The following code explains the use of cache synch dependency. An item in clustered cache 'myreplicatedcache' is replicated in local cache 'mycache' with cache sync dependency. Any change in clustered cache will automatically remove dependent item in local cache.
 
Note that for cache sync dependency, item must exist in cache before another item can be added with dependency on it.
 
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);
 
//Removing/Deleting item from cache1 will expire it from cache2 as well
catch (OperationFailedException e)
{
    // handle exception if any cache operation fails
 
}
 
See Also