Note
This feature is only available in the NCache Enterprise Edition.
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.
Note
Key dependencies can be configured between the CacheItem
and data structures as well.
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: Cache, DataStructureAttributes, CacheItemPriority, setPriority, ExpirationType, setExpiration, setDependency, KeyDependency, getDataStructuresManager, createHashSet.
- 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: Cache, DataStructureAttributes, CacheItemPriority, setPriority, Expiration, ExpirationType, setExpiration, KeyDependency, setDependency, getDataStructuresManager, createList.
Use Invalidation Attributes with Data Structures
The following code sample creates a set OrderIDSet that is dependent on a Product object in the cache. 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.
try
{
// Pre-conditions: 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
}
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
}
try {
// Pre-conditions: Cache must be connected
// Product exists in cache with key "Product:1001"
String productKey = "Product:1001";
// Specify attributes for HashSet
var attributes = new DataStructureAttributes();
// Specifiy priority for eviciton
attributes.setPriority(CacheItemPriority.High);
// Specify Absolute expiration of 15 minutes
var expiration = new Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(15));
attributes.setExpiration(expiration);
// Specify key dependency of orderKey on customerKey
var keyDependency = new KeyDependency(productKey);
attributes.setDependency(keyDependency);
// Create HashSet which is dependent on product:1001
// Specify unique cache key for set
String orderKey = "OrderIDSet";
var writeThruOptions = null;
// Create HashSet
DistributedHashSet orderIDSet = InitializeCache.cache.getDataStructuresManager().createHashSet(orderKey, attributes, writeThruOptions, Integer.class);
// 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.getErrorCode() == NCacheErrorCodes.KEY_ALREADY_EXISTS) {
// An item with the same key already exists
} else if (ex.getErrorCode() == 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 IllegalArgumentException or NullPointerException
}
try {
// Pre-conditions: Cache must be connected
// Product exists in cache with key "Product:1001"
val productKey = "Product:1001"
// Specify attributes for hashset
val attributes = new DataStructureAttributes
// Specify priority for eviction
attributes.setPriority(CacheItemPriority.High)
// Specify Absolute expiration of 15 minutes
val expiration = Expiration(ExpirationType.Absolute, TimeSpan.FromMinutes(15))
attributes.setExpiration(expiration)
// Specify key dependency of orderKey on customerKey
val keyDependency = new KeyDependency(productKey)
attributes.setDependency(keyDependency)
// Create hashset which is dependent on product:1001
// Specify unique cache key for set
val orderKey = "OrderIDSet"
val writeThruOptions = null
val orderIDSet = cache.getDataStructuresManager.createHashSet(orderKey, attributes, writeThruOptions, classOf[Integer])
// Check if the orderIDSet exists in cache:
// After 15 minutes or
// If product is modified in cache
}
catch {
case exception: Exception => {
// Handle any errors
}
}
// This is an async method
try {
// Pre-conditions: Cache must be connected
// Product exists in cache with key "Product:1001"
var productKey = "Product:1001";
// Specify attributes for list
var attributes = new ncache.DataStructureAttributes();
// Specifiy priority for eviciton
attributes.setPriority(ncache.CacheItemPriority.High);
// Specify Absolute expiration of 15 minutes
var expiration = new ncache.Expiration(
ncache.ExpirationType.Absolute,
ncache.TimeSpan.FromMinutes(15)
);
attributes.setExpiration(expiration);
// Specify key dependency of orderKey on customerKey
var keyDependency = new ncache.KeyDependency(productKey);
attributes.setDependency(keyDependency);
// Create list which is dependent on product:1001
// Specify unique cache key for list
var orderKey = "OrderIDSet";
var orderIDList = await (
await this.cache.getDataStructuresManager()
).createList(orderKey, Number(), attributes, null);
// Check if the orderIDSet exists in cache:
// After 15 minutes or
// If product is modified in cache
} catch (error) {
// Handle errors
}
try:
# Pre-conditions: Cache must be connected
# Product exists in cache with key "Product:1001"
product_key = "Product:1001"
# Specify attributes for list
attributes = DataStructureAttributes()
# Specify priority for eviction
attributes.set_priority(ncache.CacheItemPriority.HIGH)
# Specify Absolute expiration of 15 minutes
expiration = ncache.Expiration(ncache.ExpirationType.ABSOLUTE, ncache.TimeSpan.from_minutes(15))
attributes.set_expiration(expiration)
# Specify key dependency of orderKey on customerKey
key_dependency = ncache.KeyDependency(product_key)
attributes.set_dependency(key_dependency)
# Create list which is dependent on product:1001
# Specify unique cache key for list
order_key = "OrderIDSet"
order_id_list = cache.get_data_structures_manager().create_list(order_key, int, attributes)
# Check if the orderIDSet exists in cache:
# After 15 minutes or
# If product is modified in cache
except Exception as exp:
# Handle errors
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 configuring invalidation attributes with data structures on GitHub.
See Also
Using Counter in Cache
Configure Searchable Attributes
Query on Data Structures
Remove Data Structure from Cache