• Facebook
  • Twitter
  • Youtube
  • LinedIn
  • RSS
  • Docs
  • Comparisons
  • Blogs
  • Download
  • Contact Us
Download
Show / Hide Table of Contents

Remove Data with Named Tags

The items added with Named Tags to the cache can also be removed from the cache using the same Named Tags. Additionally, like 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. To remove Named Tag via CacheItem, the Named Tag property of the CacheItem is set as null.

Prerequisites

Before using the NCache Client-side APIs, ensure that the following prerequisites are fulfilled:

  • .NET
  • Java
  • Python
  • Node.js
  • Install the following NuGet packages in your .NET client application:
    • Enterprise: Alachisoft.NCache.SDK
    • Open Source: Alachisoft.NCache.Opensource.SDK
  • Include the following namespaces in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Exceptions
    • Alachisoft.NCache.Runtime.Caching
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: ICache, CacheItem, NamedTags, GetCacheItem, Insert, Remove.
  • Add the following Maven dependencies for your Java client application in pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache Enterprise-->
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • Import the following packages in your Java client application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
    • import com.alachisoft.ncache.runtime.caching.*;
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, CacheItem, NamedTagsDictionary, getCacheItem, insert.
  • Install the following packages in your Python client application:
    • Enterprise: ncache-client
  • Import the following packages in your application:
    • from ncache.client import*
    • from ncache.runtime.caching import *
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, CacheItem, insert, get_cacheitem, set_named_tags, NamedTagsDictionary.
  • Install and include the following module in your Node.js client application:
    • Enterprise: ncache-client
  • Include the following class in your application:
    • Cache
    • NamedTagsDictionary
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • For API details, refer to: Cache, CacheItem, NamedTagsDictionary, getCacheItem, insert, setNamedTags.

The following example removes the Named Tags via CacheItem:

  • .NET
  • Java
  • Python
  • Node.js
try
{
  // Precondition: 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
{
  // Precondition: 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.getNamedTags().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 generic exception like IllegalArgumentException or NullPointerException 
}  
try:
    # Precondition: Cache is already connected

    # A user wants to end the VIP membership discount offer
    # So this Named Tag needs to be removed.

    customer_key = "Customer:ALFKI"

    # Retrieve the CacheItem using the key
    cache_item = cache.get_cacheitem(customer_key)

    # Set the Named Tags 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(customer_key, cache_item)

except Exception as error:
    # Exception can occur due to:
    # Connection Failures
    # Operation Timeout
    # Operation during state transfer
    print("An error occurred:", str(error))
try
{
  // This is an async method
  // Precondition: Cache is already connected

  // A user wants to end the VIP membership discount offer
  // So this Named Tag needs to be removed.

  var customerKey = "Customer:ALFKI";

  // Retrieve the CacheItem using the key
  var cacheItem = await this.cache.getCacheItem(customerKey);

  // Set the Named Tags property of the cacheItem as null
  cacheItem.setNamedTags(null);

  // Reinsert the cacheItem in the cache with Named Tags removed
  await this.cache.insert(customerKey, cacheItem);
}
catch (error)
{
    // 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

The cacheItem.NamedTags.Remove(tagName) method will remove the Named Tag associated with the tagName. If all Named Tags are removed from the Named Tags dictionary, it becomes empty. When the cache item is inserted into the cache with this empty dictionary, it will not retain any Named Tags.

Additional Resources

NCache provides a sample application for NamedTags on GitHub.

See also

.NET: Alachisoft.NCache.Runtime.Caching namespace.
Java: com.alachisoft.ncache.runtime.caching namespace.
Python: ncache.runtime.caching class.
Node.js: NamedTagsDictionary class.

Contact Us

PHONE

+1 214-619-2601   (US)

+44 20 7993 8327   (UK)

 
EMAIL

sales@alachisoft.com

support@alachisoft.com

NCache
  • Edition Comparison
  • NCache Architecture
  • Benchmarks
Download
Pricing
Try Playground

Deployments
  • Cloud (SaaS & Software)
  • On-Premises
  • Kubernetes
  • Docker
Technical Use Cases
  • ASP.NET Sessions
  • ASP.NET Core Sessions
  • Pub/Sub Messaging
  • Real-Time ASP.NET SignalR
  • Internet of Things (IoT)
  • NoSQL Database
  • Stream Processing
  • Microservices
Resources
  • Magazine Articles
  • Third-Party Articles
  • Articles
  • Videos
  • Whitepapers
  • Shows
  • Talks
  • Blogs
  • Docs
Customer Case Studies
  • Testimonials
  • Customers
Support
  • Schedule a Demo
  • Forum (Google Groups)
  • Tips
Company
  • Leadership
  • Partners
  • News
  • Events
  • Careers
Contact Us

  • EnglishChinese (Simplified)FrenchGermanItalianJapaneseKoreanPortugueseSpanish

  • Contact Us
  •  
  • Sitemap
  •  
  • Terms of Use
  •  
  • Privacy Policy
© Copyright Alachisoft 2002 - . All rights reserved. NCache is a registered trademark of Diyatech Corp.
Back to top