Remove Data with Named Tags
Note
This feature is only available in NCache Enterprise Edition.
The items added with named tags to the cache can also be removed from the cache using the same named tag.
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.
In order 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: Cache, CacheItem, NamedTags, GetCacheItem, Insert, Remove
The following example removes the Named Tags via CacheItem
.
try
{
// Pre-condition: 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);
}
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
}
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
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 cache, named tags will be removed from that cache item since empty Named Tags dictionary indicates no named tags.
Additional Resources
NCache provides sample application for Tags on GitHub.
See also
Add/Update Items with Tags
Retrieving Items with Tag
SQL Query with NamedTags
SQL Delete with NamedTags