Configure Invalidation Attributes for Data Structures in Cache
This feature is only available in the NCache Enterprise.
The data structures can be invalidated from the cache using:
These attributes are specified during the data structure creation using the
DataTypeAttributes class and can not be modified once a data structure is created in the cache. Moreover, these invalidation attributes can be specified against all data structures.
Key dependencies can be configured between the
CacheItem and data structures as well.
- 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, DataTypeAttributes, CacheItemPriority, ExpirationType, KeyDependency, IDistributedHashSet, IDataTypeManager, CreateHashSet.
Use Invalidation Attributes with Data Structures
The following code sample creates a set OrderIDSet that is dependent on a Product object in the cache. The OrderList is assigned an expiration value of 15 minutes and its priority is set to high. If the Product is modified in the cache, OrderIDSet is removed from the cache.
// Preconditions: Cache must be connected // Product exists in cache with key "Product:1001" string productKey = "Product:1001"; // Specify attributes for HashSet var attributes = new DataTypeAttributes(); // Specifiy priority for eviciton attributes.Priority = CacheItemPriority.High; // Specify Absolute expiration of 15 minutes var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(15)); attributes.Expiration = expiration; // Specify key dependency of orderKey on customerKey var keyDependency = new KeyDependency(productKey); attributes.Dependency = keyDependency; // Create HashSet which is dependent on product:1001 // Specify unique cache key for set string orderKey = "OrderIDSet"; IDistributedHashSet<int> orderIDSet = cache.DataTypeManager.CreateHashSet<int>(orderKey, attributes); // Check if the orderIDSet exists in cache: // After 15 minutes or // If product is modified in cache
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 configuring invalidation attributes with data structures on GitHub.