Items added to the cache with specific Tags can later be removed using those same Tags, enabling selective cache invalidation without clearing the entire cache. NCache allows users to remove tagged cache items along with their values and supports removal by a single Tag, any of multiple Tags, or all specified Tags, providing flexible and efficient cache management.
Prerequisites
- Install either of the following NuGet packages in your .NET client application:
- Enterprise:
Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
- Professional:
Install-Package Alachisoft.NCache.Professional.SDK -Version 4.9.1.0
- Create a new Console Application.
- Make sure that the data being added is serializable.
- Add NCache References by locating
%NCHOME%\NCache\bin\assembly\4.0 and adding Alachisoft.NCache.Web and Alachisoft.NCache.Runtime as appropriate.
- Include the
Alachisoft.NCache.Web.Caching and Alachisoft.NCache.Runtime.Caching namespaces in your application.
- To learn more about the NCache Legacy API, please download the NCache 4.9 documents available as a .zip file on the Alachisoft Website.
How to 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 explained below.
Note
These three APIs do not return anything after removal.
How to Remove By Single 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);
// 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.getSearchService().removeByTag(tag);
# Precondition: Cache is already connected
# Create a Tag object
tag = Tag("East Coast Customers")
# Cache items containing the Tag 'East Coast Customers' are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tag(tag)
// Precondition: Cache is already connected
// This is an async method
// Create a Tag object
var tag = "East Coast Customers";
// Cache items containing the Tag 'East Coast Customers' are removed from the cache
var searchService = await this.cache.getSearchService();
await searchService.removeByTag(tag);
// Using NCache Enterprise 4.9.1
// 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.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.
How to 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.
// Precondition: Cache is already connected
// 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);
// Precondition: Cache is already connected
// Create an array of Tags to search
List<Tag> tags = List.of(
new Tag("West Coast Customers"),
new Tag("East Coast Customers")
);
// Cache items containing any of the Tags are removed from the cache
cache.getSearchService().removeByTags(tags, TagSearchOptions.ByAnyTag);
# Precondition: Cache is already connected
# Create an array of Tags
tags = [
Tag("West Coast Customers"),
Tag("East Coast Customers")
]
# Cache items containing any of the Tags are removed from the cache
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, TagSearchOptions.BY_ANY_TAG)
// Precondition: Cache is already connected
// This is an async method
// Create an array of Tags
var tags = [
new ncache.Tag("West Coast Customers"),
new ncache.Tag("East Coast Customers")
];
// Cache items containing any of the Tags are removed from the cache
var searchService = await this.cache.getSearchService();
await searchService.removeByTags(tags, ncache.TagSearchOptions.ByAnyTag);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected
// 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.RemoveByAnyTag(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.
// Precondition: Cache is already connected
// 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
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAllTags);
// Precondition: Cache is already connected
// 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
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");
cache.getSearchService().removeByTags(List.of(tags), TagSearchOptions.ByAllTags);
# Precondition: Cache is already connected
# 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
tags = [
Tag("West Coast Customers"),
Tag("East Coast Customers")
]
search_service = cache.get_search_service()
search_service.remove_by_tags(tags, TagSearchOptions.BY_ALL_TAGS)
// Precondition: Cache is already connected
// 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
// This is an async method
var tags = [
new ncache.Tag("West Coast Customers"),
new ncache.Tag("East Coast Customers")
];
var searchService = await this.cache.getSearchService();
await searchService.removeByTags(tags, ncache.TagSearchOptions.ByAllTags);
// Using NCache Enterprise 4.9.1
// User wants to remove all the customers from the East and West Coast Region
// Precondition: Cache is already connected
// Cache items containing all of the Tags are removed from the cache
Tag[] tags = new Tag[2];
tags[0] = new Tag("West Coast Customers");
tags[1] = new Tag("East Coast Customers");
cache.RemoveByAllTags(tags);
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.