Remove Data with Named Tags From Data Cache
The items added with Named Tags to the data cache can also be removed from the data 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 data cache. To remove Named Tag via CacheItem
, the Named Tagproperty
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 tag name tagName
. If all Named Tags are removed from the Named Tags dictionary and the cache item is then inserted into the cache, Named Tags will be removed from that cache item since an empty Named Tags dictionary indicates no Named Tags.
Additional Resources
NCache provides a sample application for Tags on GitHub.
See also
.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Node.js: NamedTagsDictionary class.
Python: ncache.runtime.caching class.