• Webinars
  • Docs
  • Download
  • Blogs
  • Contact Us
Try Free
Show / Hide Table of Contents

Remove Named Tags From Cache Data

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

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: Cache, CacheItem, NamedTags, GetCacheItem, Insert, Remove
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: Cache, CacheItem, NamedTagsDictionary, getCacheItem, insert.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details refer to: Cache, CacheItem, NamedTagsDictionary.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details, refer to: Cache, CacheItem, NamedTagsDictionary, getCacheItem, insert, setNamedTags.
  • To learn about the standard prerequisites required to work with all NCache client side features please refer to the given page on Client Side API Prerequisites.
  • For API details refer to: Cache, CacheItem, insert, get_cacheitem, set_named_tags, NamedTagsDictionary.

The following example removes the Named Tags via CacheItem.

  • .NET/.NET Core
  • Java
  • Scala
  • Node.js
  • Python
try
{   
    // Pre-condition: Cache is already connected

    // A User wants to end the VIP membership discount offer 
    // So this named tag needs to be removed.

    string customerKey = $"Customer:ALFKI";

    // Retrieve the CacheItem using the key
    CacheItem cacheItem = cache.GetCacheItem(customerKey);

    //Remove tag from dictionary
    cacheItem.NamedTags.Remove("VIP_Membership_Discount");

    // Reinsert the cacheItem in the cache with named tags removed
    cache.Insert(customerKey, cacheItem);
}
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 
}
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
    }
}
// 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
Note

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

Back to top Copyright © 2017 Alachisoft