Distributed cache is becoming very popular because it is a powerful way to boost your application performance and scalability and handle extreme transaction load without slowing down. Both .NET and Java applications are using it more and more each day.
But, one challenge people face with distributed cache is how to map and store relational data in a HashTable (key, value) pairing storage that a distributed cache is. Most caches today do not provide any mechanism to handle this. Today, I will discuss Data Dependency which ASP.NET Cache provides, and that NCache incorporated from day one.
Just like ASP.NET Cache, in NCache, Data Dependency allows you to specify a dependency in the distributed cache between two cached items. Cached item A depends on cached item B. And, if B is ever updated or removed from the distributed cache, A is automatically removed. This ensures that if there is a referential integrity constraint between A and B in the database that it is also honored in the distributed cache. You can also specify cascading Data Dependency where A depends on B and B depends on C. Then, if you update or remove C, A and B are both removed. Here is a brief example of Data Dependency:
Data Dependency lets you create one-to-one, one-to-many, and many-to-many relationships in the distributed cache. Here is how you would handle different scenarios:
A has one-to-one with B. Add B without any Data Dependency. Then, add A and specify a Data Dependency for B. If A and B both have a mutual dependency, then when update B afterwards and specify a dependency on A.
A has one-to-many with B. Add A first without any Data Dependency. Then, add one or more B items and specify a Data Dependency for the given A for all of them. This way, if A is updated or removed, all of B’s will be removed automatically by NCache.
A and B have many-to-many with each other. Add one or more A’s. Then, add one or more B’s and specify Data Dependency for the appropriate A’s. Then, go back and update A’s to specify Data Dependency on appropriate B’s.
Here is a simple example of creating one-to-one dependency in cache.
public static void CreateDependencies(ICache _cache)
string keyB = "objectB-1000";
Object objB = new Object();
string keyA = "objectA-1000";
Object objA = new Object();
// Initializing cacheItems
var itemOne = new CacheItem(objA);
var itemTwo = new CacheItem(objB);
// Adding objA dependent on ObjB
itemOne.Dependency = new KeyDependency(keyB);
//Adding items to cache
// Removing "objB" automatically removes “objA” as well
catch (Exception e)
So, NCache allows you to take advantage of Data Dependency and specify data relationships in the distributed cache. Download a fully working 60-day trial of NCache Enterprise and try it out for yourself.