Remove Tags in Data Cache
The items that are added to the data cache with particular tags can also be removed from the data cache using the same tags. The user can remove tagged items along with their values from the data cache.
Prerequisites for Removing 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, Tag, RemoveByTag, RemoveByTags, RemoveBulk, SearchService.
Remove Tagged Data from Cache
Similar to retrieving items using tags, tagged data can be removed using either the RemoveByTag
or RemoveByTags
method containing TagSearchOptions
(ByAllTags
and ByAnyTag
). These methods are then explained below.
Note
These three APIs do not return anything after removal.
Remove By Tag
RemoveByTag
is a method in which a single tag is provided and the items associated with that particular tag are removed from the cache.
The following example removes the cache items associated with the tag East Coast Customers.
Tip
One quick way to verify the successful removal of items from the cache is to check the item count by using the Count
property of the cache before and after operating.
// Precondition: Cache is already connected
// Create a tag object
Tag tag = new Tag("East Coast Customers");
// Cache items containing the tag 'East Coast Customers' are removed from the cache
cache.SearchService.RemoveByTag(tag);
Note
To ensure the operation is fail-safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Remove By Any Tag
RemoveByTags
is a method and ByAnyTag
is the search option in which a tag list of multiple tags is provided and the items containing ANY of the tags are removed from the cache.
The following example removes the items from the cache containing any tag from the list tags
.
// Create an array of tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");
// Cache items containing any of the tags are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAnyTag);
Remove By All Tags
RemoveByTags
is a method and ByAllTags
is the search option in which a list of tags can be provided and the items containing all the tags provided in the list will be removed from the cache.
The following example removes the items from the cache containing all tags from the list tags
.
// User wants to remove all the customers from the East and West Coast Region
// Cache items containing all of the tags are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAllTags);
Warning
Providing Null
tag array will throw an ArgumentNullException or NullPointerException.
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.