Aggregate Cache Dependency
Note
This feature is only available in NCache Enterprise Edition.
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
- Install the following NuGet packages in your application:
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Dependencies
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- For API details, refer to: ICache, CacheItem, Dependency, AggregateCacheDependency.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
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 a CacheItem
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 = $"Product:1001";
// Get product from database against given ProductID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key1 = "Product:" + product.ProductID;
// Create a new CacheItem with product
var cacheItem = new CacheItem(product);
// Specify the path where the master file is placed
string filepath = "D:\\ProductList.csv";
// Initializing Aggregate Dependency
var 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
cacheItem.Dependency = aggregateDependency;
cache.Insert(key1, cacheItem);
// For successful addition of item with dependency
// Update or remove the master key or file
// Verify if the dependent key is present
}
catch (OperationFailedException ex)
{
// NCache specific exception
if (ex.ErrorCode == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
{
// The dependent item does not exist in the cache
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
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.
Additional Resources
NCache provides sample application for File Dependency on GitHub.
See Also
Key Dependency Types and Usage
Multi Cache Key Dependency
Data Expiration
Cache Data Dependency on External Source