NCache 4.6 - 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);
 
                //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 from cache2 successfully
                }
                else
                {
                    //item not removed from cache2
                }
            }
            catch (OperationFailedException e)
            {
                // handle exception if any cache operation fails
            }
 
 
See Also