Alachisoft NCache 4.1 - Online Documentation

Key Dependency

NOTE: This feature is not available in NCache Express edition.
Key based dependency associates one cached item with another item in the cache. It invalidates the dependent item when that particular item changes. Whenever that item is removed or updated from the cache, associated object will be expired. Key based dependency is cascaded. For example, if key1 depends on key2, key2 depends on key3 and key3 depends on key4, then removal of key4 will result in expiration of key1, key2 and key3. It is not clean interval dependent.
Add the following namespaces in your project:
using Alachisoft.NCache.Web.Caching;
using Alachisoft.NCache.Runtime;
using Alachisoft.NCache.Runtime.Dependencies;
The following code explains the implementation.
    Cache _cache = NCache.InitializeCache("mycache");
    _cache.Add("Key-1", "Key Value");
    String[] cacheKeys = new String[1];
    cacheKeys[0] = "Key-1";
    //Adding cache item "Key-2" dependant on another cache item.
    _cache.Add("Key-2", "dependant item", new KeyDependency(cacheKeys), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
    //Removing/Deleting/Updating key: "Key-1" will automatically make all of its dependant keys removed, here it's "Key-2"
    Console.WriteLine("Removing Key-1");
    Object removedObject = _cache.Remove("Key-1");
    Console.WriteLine("value /" + removedObject.ToString() + "/ removed");
    if (_cache.Contains("Key-2"))
        Console.WriteLine("ERROR: cache Key: key-2 not removed,/n Please read Help from NCache for further details or contact");
        Console.WriteLine("Cache Key: Key-2 does not exist, item removed once the base key was removed from Cache");
See Also
Copyright © 2005-2012 Alachisoft. All rights reserved.