Configure Invalidation Attributes for Data Structures in Cache
Note
This feature is only available in NCache Enterprise Edition.
Data Structures can be invalidated from the cache using:
- Expiration
- Eviction
- Dependency
These attributes are specified during data type creation using the DataTypeAttributes class, and can not be modified once a data type is created in cache. Moreover, these invalidation attributes can be specified against all data structures.
Note
Key dependencies can be configured between CacheItem and Data Structures as well.
Pre-requisites
- Include the following namespaces in your application:
Alachisoft.NCache.Client.DataTypes
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Exceptions
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Caching
- The application must be connected to cache before performing the operation.
- Cache must be running.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
The following code sample creates a set OrderIDSet which is dependent on a Product object in cache. OrderList is assigned an expiration value of 15 minutes and its priority is set to high. If the Product is modified in cache, OrderIDSet is removed from cache.
try
{
// Pre-conditions: Cache must be connected
// Product exists in cache with key "Product:1001"
// Product with this key exists in cache
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
}
catch (OperationFailedException ex)
{
// NCache specific exception
if(ex.ErrorCode == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else if (ex.ErrorCode == NCacheErrorCodes.CACHEITEM_IN_DATA_STRUCTURES)
{
// Data structures cannot be of CacheItem type
// CacheItems cannot be added in data structures
}
else
{
// NCache specific exception
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
See Also
List Behavior and Usage in Cache
Queue Behavior and Usage in Cache
Sets Behavior and Usage in Cache
Dictionary Behavior and Usage in Cache
Using Counter in Cache
Configure Searchable Attributes
Configure Invalidation Attributes
Query on Data Structures
Remove Data Structure from Cache