Add/Update Tags in Data Cache
NCache allows the user to create tags and then add items to the cache with tags. The user can also add a tag as a CacheItem
. Moreover, you can update and append new tags to an already existing tag list.
Prerequisites to Add/Update Tags in Data Cache
- 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: ICache, CacheItem, CacheItemVersion, Tag, Insert, Add,Tags, Count, Contains.
Tag Data
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 a property
of the Tags class.
Warning
- If the key already exists, an exception will be thrown.
- Providing a
Null
tag array will throw an ArgumentNullException.
The following example adds a CacheItem
containing the object customer to the cache using the Add
API. It then sets an additional tag property against it by adding the tags.
// Precondition: Cache is already connected
// A VIP Customer from the East Coast region has logged into an e-commerce website.
// The customer will be added to the cache according to the category tags.
string customerKey = $"Customer:ALFKI";
Customer customer = cache.Get<Customer>(customerKey);
// Get customer from database if not found in cache
if (customer == null)
{
// Get customer from database
customer = FetchCustomerFromDB("ALFKI");
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Create an array of tags assigned to customer
Tag[] tags = new Tag[2];
tags[0] = new Tag("East Coast Customers");
tags[1] = new Tag("VIP Customers");
// Setting the tag property of the cacheItem
cacheItem.Tags = tags;
cache.Add(customerKey, cacheItem);
// CacheItem added successfully
}
Note
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 Tags
Updating tags through CacheItem
requires the item to be first fetched, its tags to be modified, and then re-inserted into the cache using the Insert method.
The following example first fetches the CacheItem
, creates a tag list with the new tags, and then reinserts the CacheItem
in the cache along with the modified tag list. Hence, overrides the value of existing tags.
// A VIP Customer has logged into an e-commerce website from a different region, i.e., West Coast.
// This customer will be added to the cache according to the category tags
// Create an array of new tags assigned to customer with different regiom
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("VIP Customers");
// Set the tag property of the cacheItem with a new updated array.
cacheItem.Tags = tags;
// Reinserts the updated cacheItem into cache with different tags
cache.Insert(customerKey, cacheItem);
Append Tags to Already Existing Tags
If there is a need to append a new tag to the already present tags or if a tag needs to be removed from the already present tags, the operation will require manipulating the tags array. For this purpose, create a new list with the tags to append and copy that list to the array containing the tags already. This way a new tag would be added to the array containing the tags previously created without affecting them.
The following example appends new tags to an already existing tag list Tags.
// A customer has logged in from the US who wants to add tags for the country and region.
string customerKey = $"Customer:ALFKI";
// Get CacheItem from Cache
CacheItem cachedCustomer = cache.GetCacheItem(customerKey);
// Create new List for tags
var customerTags = new List<Tag>();
// Initialize the list with existing tags
customerTags = new List<Tag>(cachedCustomer.Tags);
// Add new tags to existing list of tags
customerTags.Add(new Tag("US Customers"));
// Assign newly built tag list to the cached customer object.
cachedCustomer.Tags = customerTags.ToArray();
// Update the modified customer object in cache
cache.Insert(customerKey, cachedCustomer);
Additional Resources
NCache provides sample application for Tags on GitHub.
See Also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.runtime.caching class.
Node.js: Tag class.