Add Cache Data with Named Tags
Note
This feature is only available in NCache Enterprise Edition.
In order to associate Named Tags with any cache item, it is required to provide a list of Named Tags, each having two parameters that are key as string which is the name of the tag and value as any primitive type which is the assigned value to the key. NCache then allows you to associate your objects with these Named Tags. You can add items in the cache with named tags and then later retrieve these items from the cache using the previously added named tags.
When items are already added in the cache with certain named tags, NCache provides the user with an option of updating named tags already present in the cache.
Pre-Requisites
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
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.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Add Named Tags
CacheItem is a custom class provided by NCache which can be used to add data to the cache.
This class encapsulates data as its value property. CacheItem
also lets you set additional
specifications associated with an object as property of the NamedTags class.
In the following example Named Tags are set by assigning them as a property of
CacheItem
. In case of Java, the setNamedTags property is used to add named tags to the CacheItem
.
try
{
//Pre-condition: Cache is already connected
// Get product from database against given productID
Product product = FetchProductFromDB(1001);
// Create a unique cache key for this product
string key = $"Products:{product.ProductID}";
// Creating a Named Tags Dictionary
var productNamedTag = new NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the instances of data, of primitive data structures.
productNamedTag.Add("FlashSaleDiscount", 0.5);
// Create a new CacheItem
var cacheItem = new CacheItem(product);
// Setting the named tag property of the cacheitem
cacheItem.NamedTags = productNamedTag;
// Add product object to the cache
CacheItemVersion version = cache.Add(key, cacheItem);
// CacheItem added successfully
}
catch (OperationFailedException ex)
{
// NCache specific exception
if(ex.ErrorCode == NCacheErrorCodes.KEY_ALREADY_EXISTS)
{
// An item with the same key already exists
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
}
catch (Exception ex)
{
// Any other 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 Data with Named Tags through CacheItem
Updating named tags through CacheItem requires the item to be first fetched, its named tags to be modified and then re-inserted to the cache using the Insert method.
Following example first fetches the CacheItem
, creates a list with the new named tags and then re-inserts the CacheItem in the cache along with the modified named tag list. Hence, overwrites the value of existing named tags.
Warning
Providing Null
named tag array will throw an ArgumentNullException.
try
{
// Get cache item from cache against given key
string key = "Products:1002";
CacheItem cacheItem = cache.GetCacheItem(key);
// Create a Named Tags Dictionary
var newProductNamedTag = new NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the updated instances of data, of primitive data types.
newProductNamedTag.Add("FlashSaleDiscount", 0.7);
// Updates the named tags with the new named tags
// Overrides the value of the existing named tags
cacheItem.NamedTags = newProductNamedTag;
//Re-inserts the cacheItem into cache with modified named tags
CacheItemVersion version = cache.Insert(key, cacheItem);
// cacheItem is successfully added in cache with modified named tags
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any other 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 Named tags at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\GroupsAndTags
See Also
Tag Cache Data
SQL Query with NamedTags
SQL Delete with NamedTags
Data Expiration