Aggregate Cache Dependency
This feature is only available in NCache Enterprise.
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 the 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.
Add Data to Cache with Aggregate Dependency
You can add items to your cache using the Aggregate Dependency. 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.
// Precondition: 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 dependentKey = "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(dependentKey, cacheItem); // For successful addition of item with dependency // Update or remove the master key or file // Verify if the dependent key is present
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
NCache provides a sample application for File Dependency on GitHub.