Aggregate Dependency
NCache also allows you to use different strategies in combination with the same cache data in the form of Aggregate Cache Dependency. The 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.
The 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 on a file placed at some path outside the cache. Since the item is dependent on both, the key of the other item and a gile, the Aggregate Dependency can be used here. It lets you add both Key and File Dependency to the item.
The 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. 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. Combining different invalidation strategies provides you with more flexible ways to meet your application needs in different environments.
Prerequisites
- To learn about the standard prerequisites required to work with all NCache client-side features, please refer to the given page on Client-Side API Prerequisites.
- For API details, refer to: ICache, CacheItem, Dependency, AggregateCacheDependency, FileDependency, KeyDependency.
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 on 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
Note
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 a sample application for File Dependency on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Dependencies namespace.
Java: com.alachisoft.ncache.runtime.dependencies namespace.