Add/Update Cache Data with Groups
Note
This feature is only available in NCache Enterprise Edition.
NCache allows the user to add an item in cache with a certain group. There can only be a single item associated with a single group.
CacheItem
is a custom class provided by NCache which can be used to add data to the cache. CacheItem
also lets you set
additional specifications associated with an object as property
of Groups.
CacheItem is added in the cache using the Insert
method is a recommended approach with group if no item already exists in the cache with the specified key.
Whereas the Add
operation fails if the key already exists and an exception will be thrown.
Pre-Requisites
- Install the following NuGet package in your application.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- For API details refer to: ICache, CacheItem, Insert, property(setting group), GetCacheItem()
- 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.
In the following example, data group is set by assigning it as a property of CacheItem
containing the object customer. For Java, data group is assigned by using the setGroup property of the CacheItem.
try
{
// Pre-condition: Cache is already connected
// Get customer from database against given customer ID
Customer customer = FetchCustomerFromDB("ALFKI");
// Create a unique cache key for this customer.
string key = $"Customers:{customer.CustomerID}";
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Specify the group
cacheItem.Group = "Important Customers";
// Add customer object to cache with group
CacheItemVersion version = cache.Insert(key, cacheItem);
// cacheItem is successfully added in the cache with group
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Update Group of a CacheItem
Data group of a CacheItem
can also be updated. CacheItem
is a custom class provided by NCache which can be used to add data to the cache. CacheItem
also lets you set group as property
of the CacheItem
.
The following example updates group in a CacheItem
.
try
{
// Pre-condtion: CacheItem is added with the group
// Retrieve the cacheItem from the cache
string key = "ALFKI";
var cacheItem = cache.GetCacheItem(key);
// Set the group property of cache as null
cacheItem.Group = null;
// Re-insert the item in the cache after the removal of group
cache.Insert(key, cacheItem);
// Get cacheitem with the updated version
cacheItem = cache.GetCacheItem(key);
// Specify the new group to be updated
cacheItem.Group = "US Customers";
// Re-insert the cacheItem in the cache with updated groups
cache.Insert(key, cacheItem);
// CacheItem is successfully added in the cache with updated group
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like ArgumentNullException or ArgumentException
}
Recommendation: 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 sample application for Groups on GitHub.
See Also
Retrieve Cache Data with Groups
Remove Cache Data with Group
Basic Operations for Caching Data
Tag Cache Data