Remove Data from Cache with Tags
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 for Removing Items with Tags
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
Alachisoft.NCache.Runtime
- The application must be connected to cache before performing the operation.
- Cache must be running.
- Make sure that the data being added is serializable.
- To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
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.
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.
try
{
// 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.RemoveByTag(tags[0]);
// In oder to verify successfull removal use the Count method
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
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
RemoveByAnyTag is a method 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
.
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.
try
{
// 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 tag from the list are removed from the cache
cache.RemoveByAnyTag(tags);
// In oder to verify successfull removal use the Count method
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
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
RemoveByAllTags is a method in which a tag list of multiple tags is provided and the items containing ALL of the tags are removed from the cache.
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.
try
{
// 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 all the tags from the list are removed from the cache
cache.RemoveByAllTags(tags);
// In oder to verify successfull removal use the Count method
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
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 DeleteBulk 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
{
// 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 keys = cache.GetKeysByTag(customerTags[0]);
// Checks the key Count
if (keys.Count > 0)
{
// IDictionary containing customers mapped to keys returned when
// removed from cache
IDictionary removedItems = cache.RemoveBulk(keys);
}
else
{
// No key exists in the cache
}
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
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.
See Also
Using Groups
Retrieve Data from Cache with Tags
Add/Update Data in Cache with Tags
Search Data in Cache with SQL and Tags
Delete Data in Cache with SQL and Tags
Named Tags