Remove Data with Named Tags
The items added with Named Tags to the cache can also be removed from the cache using the same Named Tags. Additionally, like updating Named Tags, they can also be removed via CacheItem. This is a custom class provided by NCache which can be used to add data to the cache. To remove Named Tag via CacheItem, the Named Tag property of the CacheItem is set as null.
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: ICache, CacheItem, NamedTags, GetCacheItem, Insert, Remove.
The following example removes the Named Tags via CacheItem:
// Precondition: Cache is already connected
// A user wants to end the VIP membership discount offer
// So this Named Tag needs to be removed.
string customerKey = "Customer:ALFKI";
// Retrieve the CacheItem using the key
CacheItem cacheItem = cache.GetCacheItem(customerKey);
// Remove tag from dictionary
cacheItem.NamedTags.Remove("VIP_Membership_Discount");
// Reinsert the cacheItem in the cache with Named Tags removed
cache.Insert(customerKey, cacheItem);
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Note
The cacheItem.NamedTags.Remove(tagName) method will remove the Named Tag associated with the tagName. If all Named Tags are removed from the Named Tags dictionary, it becomes empty. When the cache item is inserted into the cache with this empty dictionary, it will not retain any Named Tags.
Additional Resources
NCache provides a sample application for NamedTags on GitHub.
See also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.runtime.caching class.
Node.js: NamedTagsDictionary class.