Note
This feature is only available in NCache Enterprise Edition.
The items added with Named Tags to the cache can also be removed from the cache using the
same named tag.
Similar to updating Named Tags; they can also be removed via CacheItem
. This is a custom class provided by NCache which can be used to add data to the cache.
In order to remove Named Tag via CacheItem the Named Tag property
of the CacheItem is set as null
.
Prerequisites
- Install the following NuGet package in your application:
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Exceptions
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: CacheItem, NamedTags.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.runtime.exceptions.*;
import com.alachisoft.ncache.runtime.caching.NamedTagsDictionary.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: CacheItem, NamedTags.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
- Install and include the following module in your application:
- Enterprise:
const ncache = require('ncache-client')
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: CacheItem, NamedTags.
- 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.
- Install the NCache Python client by executing the following command:
# Enterprise Client
pip install ncache-client
- Import the NCache module in your application.
- Cache must be running.
- 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.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-scala-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.scala.client.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
The following example removes the Named Tags via CacheItem
.
try
{
// Pre-condition: Cache is already connected
// CacheItem is already added in the cache with named tags with this key
string key = "Product:1001";
// Retrieve the CacheItem using the key
CacheItem cacheItem = cache.GetCacheItem(key);
// Set the named tag property of the cacheItem as null
cacheItem.NamedTags = null;
// Re-insert the cacheItem in the cache with named tags removed
cache.Insert(key, cacheItem);
// CacheItem is successfully re-inserted in cache with removed named tags
}
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
}
try
{
// Pre-condition: Cache is already connected
// CacheItem is already added in the cache with named tags with this key
String key = "Product:1001";
// Retrieve the CacheItem using the key
CacheItem cacheItem = cache.getCacheItem(key);
// Set the named tag property of the cacheItem as null
cacheItem.setNamedTags(null);
// Re-insert the cacheItem in the cache with named tags removed
cache.insert(key, cacheItem);
// CacheItem is successfully re-inserted in cache with removed named tags
}
catch (OperationFailedException ex)
{
// Exception can occur due to:
// Connection Failures
// Operation Timeout
// Operation performed during state transfer
}
catch (Exception ex)
{
// Any generic exception like IllegalArgumentException or NullPointerException
}
// This is an async method
try
{
// Pre-condition: Cache is already connected
// CacheItem is already added in the cache with named tags with this key
var key = "Product:1001";
// Retrieve the CacheItem using the key
var cacheItem = await this.cache.getCacheItem(key);
// Set the named tag property of the cacheItem as null
cacheItem.setNamedTags(null);
// Re-insert the cacheItem in the cache with named tags removed
await this.cache.insert(key, cacheItem);
// CacheItem is successfully re-inserted in cache with removed named tags
}
catch (error)
{
// Handle errors
}
try:
# Pre-condition: Cache is already connected
# CacheItem is already added in the cache with named tags with this key
key = "Product:1001"
# Retrieve the CacheItem using the key
cache_item = cache.get_cacheitem(key)
# Set the named tag property of the cacheItem as null
cache_item.set_named_tags(None)
# Re-insert the cacheItem in the cache with named tags removed
cache.insert(key, cache_item)
# CacheItem is successfully re-inserted in cache with removed named tags
except Exception as exp:
# Handle errors
try {
// Pre-condition: Cache is already connected
// CacheItem is already added in the cache with named tags with this key
val key = "Product:1001"
// Retrieve the CacheItem using the key
val cacheItem = cache.getCacheItem(key)
// Set the named tag property of the cacheItem as null
cacheItem.setNamedTags(null)
// Re-insert the cacheItem in the cache with named tags removed
cache.insert(key, cacheItem)
// CacheItem is successfully re-inserted in cache with removed named tags
}
catch {
case exception: Exception => {
// Handle any errors
}
}
Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
Note
cacheItem.NamedTags.Remove(tagName)
method will remove the Named Tag associated with the tag name tagName
. If all Named Tags are removed from the Named Tags dictionary and the cache item is then inserted into cache, Named Tags will be removed from that cache item since empty Named Tags dictionary indicates no Named Tags.
Additional Resources
NCache provides sample application for Tags on
GitHub.
See also
Add/Update Items with Tags
Retrieving Items with Tag
SQL Query with NamedTags
SQL Delete with NamedTags