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.
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);
// Pre-condition: 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.getSearchService().removeByTag(tag);
System.out.println("Items with tag 'East Coast Customers' removed successfully");
# Precondition: Cache is already connected
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Cache items containing the tag 'Important Customers' are removed from the cache
search_service = cache.get_search_service()
keys = search_service.remove_by_tag(tags[0])
// Precondition: Cache is already connected
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Cache items containing the tag 'Important Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTag(tags[0]);
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);
// Create an array of tags to search
List<Tag> tags = List.of(
new Tag("East Coast Customers"),
new Tag("West Coast Customers")
);
// Cache items containing any of the tags are removed from the cache
cache.getSearchService().removeByTags(tags, TagSearchOptions.ByAnyTag);
System.out.println("Items with either 'West Coast Customers' or 'East Coast Customers' tags removed successfully");
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Cache items containing the tag 'Important Customers' or 'East Coast Customers' are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, ncache.TagSearchOptions.BY_ANY_TAG)
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Cache items containing the tag 'Important Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTags(tags, ncache.TagSearchOptions.ByAnyTags);
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);
// 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 all of the tags are removed from the cache
cache.getSearchService().removeByTags(List.of(tags), TagSearchOptions.ByAllTags);
System.out.println("Items with both 'West Coast Customers' and 'East Coast Customers' tags removed successfully");
# Following tags are created and item is added in the cache with these tags
tags = [
ncache.Tag("Important Customers"),
ncache.Tag("East Coast Customers")
]
# Cache items containing the tag 'Important Customers' and 'East Coast Customers' are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, ncache.TagSearchOptions.BY_ALL_TAGS)
// This is an async method
// Following tags are created and item is added in the cache with these tags
var tags = new ncache.Tag[2];
tags[0] = new ncache.Tag("Important Customers");
tags[1] = new ncache.Tag("East Coast Customers");
// Cache items containing the tag 'Important Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
var keys = await searchService.removeByTags(tags, ncache.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.