Alachisoft NCache 4.1 - Online Documentation

Aggregate Dependency

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Aggregate Cache Dependency associates one cached item with a collection of dependency objects which can be CacheDependency objects, DBCacheDependency objects or any combination of these. The AggregateCacheDependency monitors a collection of dependency objects so that when any of them changes, the cached object becomes obsolete and is removed from the cache.
 
Aggregate Cache Dependency allows you to associate multiple dependencies of different types with a single cached item. For example, you can associate key dependency and file dependency with an item using aggregate dependency by creating an instance of the AggregateCacheDependency class with each dependency added to it. You can then use a single Insert call to make the item dependent on the AggregateCacheDependency instance.
 
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.Clear();
 
    Customer customer = new Customer();
    customer.name = "David";
    customer.customerID = 1001;
 
    CacheItem cItem = new CacheItem(customer);
 
    //Adding a key to create Dependency on
    _cache.Add("Customer:David:1001", cItem);
 
    //Customer Steve is dependant on David
    customer = new Customer();
    customer.name = "Steve";
    customer.customerID = 1002;
 
    //Initializing Aggregate Dependency; CacheItem dependant on File and Key
    AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();
    aggregateDependency.Dependencies.Add(new FileDependency("C:\\customer.config"));
    aggregateDependency.Dependencies.Add(new KeyDependency("Customer:David:1001"));
    
    cItem = new CacheItem(customer);
    cItem.Dependency = aggregateDependency;
 
    _cache.Add("Customer:Steve:1002", cItem);
 
    //Updating Key Customer:David:1001
    customer = new Customer();
    customer.name = "David";
    customer.customerID = 1001;
    cItem = new CacheItem(customer);
 
    _cache.Insert("Customer:David:1001", cItem);
    
    //Key will be removed since it's dependent Key has been updated
    if (!_cache.Contains("Customer:Steve:1002"))
    {
        Console.WriteLine("Cache Key: Customer:Steve:1002 does not exist, item removed once the base key or file is removed/deleted/updated from Cache");
    }
    else
    {
        Console.WriteLine("ERROR: cache Key: Customer:Steve:1002 not removed,/n Please read Help from NCache for further details or contact support@alachisoft.com");
    }
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.