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
- Install the following NuGet packages:
- 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.
- For API details, refer to: ICache, DataTypeAttributes.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>5.2.0</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.runtime.exceptions.*;
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.client.datastructures.*;
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: Cache, DataStructureAttributes.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
- Include the following modules in your application.
const ncache = require('ncache-client')
- The application must be connected to cache before performing the operation.
- Cache must be running.
- For API details, refer to: Cache, DataStructureAttributes.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
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"
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;
DistributedHashSet orderIDSet = 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
}
// 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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
See Also
Using Counter in Cache
Configure Searchable Attributes
Query on Data Structures
Remove Data Structure from Cache