Remove Cache Data with Tags
Note
This feature is only available in NCache Enterprise Edition.
The items which are added in the cache with particular tags can also be removed from the cache using the same tags. The user can remove tagged items along with their values from the cache.
Pre-Requisites
- Install the following NuGet package in your application.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Exceptions
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- For API details refer to: ICache, Tag, RemoveByTag, RemoveBulk
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Remove Tagged Data from Cache
Similar to retrieving items using tags,
tagged data can be removed using either RemoveByTag
, RemoveByAnyTag
or RemoveByAllTags
method. These methods are then explained below.
Note
These three APIs do not return anything after removal.
RemoveByTag
RemoveByTag
is a method in which a single tag is provided and the items associated with that particular tag are removed from the cache. In case of Java, removeByTag
method is used for this purpose in case of Java.
The following example removes the cache items associated with the tag Important Customers.
Tip
One quick way to verify successful removal of items from the cache is to check the item count by using the Count
property of the cache before and after performing the operation.
Warning
Providing Null
tag array will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Cache items containing the tag 'Important Customers' are removed from the cache
cache.SearchService.RemoveByTag(tags[0]);
}
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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
RemoveByAnyTag
RemoveByTag
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. For Java, the method used for this purpose is removeByAnyTag
.
The following example removes the items from the cache containing any tag from the list tags
.
Tip
One quick way to verify successful removal of items from the cache is to check the item count by using the Count
property of the cache before and after performing the operation.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important 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);
}
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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
RemoveByAllTags
RemoveByTag
is a method and ByAllTags
is the search option in which a tag list of multiple tags is provided and the items containing ALL of the tags are removed from the cache. In java, removeByAllTags
is used for removing the items matching all the tags provided.
The following example removes the items from the cache containing all tags from the list tags
.
Tip
One quick way to verify successful removal of items from the cache is to check the item count by using the Count
property of the cache before and after performing the operation.
Warning
Providing Null
tag list will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Cache items containing the tag 'Important Customers' are removed from the cache
cache.SearchService.RemoveByTags(tags, TagSearchOptions.ByAllTags);
}
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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Remove Cached Data by Retrieving Keys
Another approach to remove tagged data items is by first retrieving the keys
using tags and then by removing them using the RemoveBulk API.
The advantage of using this approach is that the successfully removed items are returned
after the removal whereas the RemoveByTag
method does not return anything.
Warning
This method is not a recommended approach as it increases the network calls which is not efficient for the user.
The following example retrieves the keys from the cache containing the specific tag using the GetKeysByTag
method. You can use any of the three methods for retrieving the keys discussed in the Retrieve Items with Tag. The retrieved keys are then removed along with the values from the cache.
// PreCondition: Tag is created and associated with an item
try
{
// Pre-condition: Cache is already connected
// Following tags are created and item is added in the cache with these tags
Tag[] tags = new Tag[2];
tags[0] = new Tag("Important Customers");
tags[1] = new Tag("East Coast Customers");
// Keys with the Tag 'ImportantCustomers' are retrieved
// Retrieved Cache keys are then returned to an ICollection
ICollection<string> keys = cache.SearchService.GetKeysByTags(tags, TagSearchOptions.ByAllTags);
// Checks the key Count
if (keys.Count > 0)
{
// Create dictionary to store removed items
IDictionary<string, Customer> removedItems = new Dictionary<string,Customer>();
// Remove items
cache.RemoveBulk(keys, out removedItems);
}
else
{
// No key exists in the cache
}
}
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
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Additional Resources
NCache provides sample application for Tags on GitHub.
See Also
Use Groups for Logical Data Grouping
Retrieve Cache Data with Tags
Add/Update Cache Data with Tags
Search Tag Data in Cache with SQL
Delete Tag Data from Cache with SQL
Named Tags