Aggregate Dependency
NCache also allows you to use different strategies in combination with the same cache data in the form of Aggregate Cache Dependency. 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 and data will be invalidated based on whichever dependency triggers first.
Following are the dependencies that can be added using aggregate dependencies:
- Key Dependency
- File Dependency
- Database Cache Dependency
- Notification Based Dependency
- Polling based Dependency
- Notification Based Dependency
You can add any of above dependencies together using aggregate dependency. All of these dependencies are explained in the successive chapters.
For example, there is a case when an item is dependent on another item in the cache as well as a file placed at some path outside the cache. Since the item is dependent on both the key of the other item and a file, aggregate dependency can be used here. It lets you add both key and file dependency to the item.
Aggregate Cache Dependency associates one cached item with a collection of
dependent objects which can be any other dependency provided by NCache e.g.
KeyDependency
, DBCacheDependency
or any combination of these.
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.
Combining different invalidation strategies provides you with more flexible ways to meet your application needs in different environments.
Pre-Requisites for Using Aggregate Dependency
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Dependencies
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Add Data to Cache with Aggregate Dependency
You can use Add method to add aggregate dependency to an item in the cache.
Important
Note that this API also specifies cache item priority for eviction as well as expiration, so the value for that parameter has been passed as Default
, as it is not discussed here.
The following example adds an item to the cache against a key that is dependent on another item in the cache as well as a file named productList.csv placed at the specified path using aggregate dependency.
try
{
// Pre-condition: Cache is already connected
// Item with the given key exists in the cache
string key = "1001";
// Get product from database against given ProductID
Product product = new FetchProductFromDB("1002");
// Generate a unique cache key for this product
string key1 = $"Product:{product.ProductID}";
// Specify the path where the master file is placed
string filepath = "D:\\ProductList.csv";
// Initializing Aggregate Dependency
AggregateCacheDependency aggregateDependency = new AggregateCacheDependency();
// Adding file and key dependency in aggregate dependency
aggregateDependency.Dependencies.Add(new FileDependency(filepath));
aggregateDependency.Dependencies.Add(new KeyDependency(key));
// Adding new item with AggregateCacheDependency
cache.Insert(key1, product, aggregateDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
// For successful addition of item with dependency
// Update or remove the master key or file
// Verify if the dependent key is present using:
// cache.Contains()
// cache.Count
}
catch (OperationFailedException ex)
{
// NCache specific exception
if (ex.Message.Contains("One of the dependency keys does not exist."))
{
// If the master key doesnt exist in the cache
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
See Also
Key Dependency
Multi Cache Key Dependency
Data Expiration
Sync Cache with External Source