Alachisoft NCache 4.1 - Online Documentation

Aggregate Dependency

 
Aggregate Cache Dependency associates one cached item with collection of dependency objects which can be CacheDependency objects, DBCacheDependency objects or any combination of these. The AggregateCacheDependency monitors 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 import statement in your project:
 
import com.alachisoft.ncache.web.caching.*;
import com.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.Id = 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.Id = 1002;
cItem = new CacheItem(customer);
 
//Initializing Aggregate Dependency; CacheItem dependent on File and Key
AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();
aggregateDependency.getDependencies().add(new FileDependency("C:\\customer.config"));
aggregateDependency.getDependencies().add(new KeyDependency("Customer:David:1001"));
cItem.setCacheDependency(aggregateDependency);
 
_cache.add("Customer:Steve:1002", cItem);
 
//Updating Key Customer:David:1001
customer = new Customer();
customer.Name = "David";
customer.Id = 1001;
cItem = new CacheItem(customer);
_cache.insert("Customer:David:1001", cItem);
 
Thread.sleep(1000);
 
//Key will be removed since it's dependant Key has been updated
if (!_cache.contains("Customer:Steve:1002"))
{
    System.out.println("Cache Key: Customer:Steve:1002 does not exist, item removed once the base key or file is removed/deleted/updated from Cache");
}
else
{
    System.out.println("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.