Try Playground
Show / Hide Table of Contents

Multi-Cache Sync Dependency Usage [Deprecated]

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

  • .NET
  • Java
  • Legacy API
  • 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.
  • 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: Cache, CacheItem, setSyncDependency, getCache.
  • Create a new Console Application.
  • Make sure that the data being added is serializable.
  • Add NCache References by locating %NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
  • Include the Alachisoft.NCache.Runtime.Dependencies namespace in your application.

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.

  • .NET
  • Java
  • Legacy API
// 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
// Connect to the caches for cachesync dependency
Cache cache1 = CacheManager.getCache("demoCache");
Cache cache2 = CacheManager.getCache("demoClusteredCache");

// Insert the item in demoCache
dataCache = CacheManager.getCache("demoCache");

sessionCache = CacheManager.getCache("demoClusteredCache");
Product product = Product.fetchProductFromDB("Product:1001");
String key = "Product:1001";
dataCache.insert(key,product);

// Set the cachesync dependency for this key
var dependency = new CacheSyncDependency("demoCache",key);
var cacheItem = new CacheItem(key);
cacheItem.setSyncDependency(dependency);

// Insert the item with the same key in demoClusteredCache
sessionCache.insert(key,cacheItem);
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
}
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.

Contact Us

PHONE

+1 (214) 764-6933   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • NCache Enterprise
  • NCache Professional
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top