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

Delete Data with Named Tags using SQL Query

NCache provides object queries to delete the particular item from the cache having the Named Tag along with its value. Using the ExecuteNonQuery API, you can perform efficient data removal based on these tags. Ensure that the item is added to the cache with the Named Tags. Refer to the Add Items with Named Tags section to get details on creating and adding Named Tags.

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
    • Alachisoft.NCache.Client.Services
  • The cache must be running.
  • Make sure that the data being added is serializable.
  • Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
  • For API details, refer to: ICache, ICacheReader, QueryCommand, Remove, ExecuteNonQuery, SearchService.
  • 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.
  • Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
  • For API details, refer to: Cache, remove, executeNonQuery, QueryCommand, getSearchService.
  • 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 *
    • from ncache.client.services import *
  • The cache must be running.

  • Make sure that the data being added is serializable.
  • Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
  • For API details, refer to: Cache, getSearchService, QueryCommand, execute_non_query.
  • 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.
  • Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
  • For API details, refer to: Cache, remove, executeNonQuery, QueryCommand, getSearchService.

Syntax

The following example shows how to delete items associated with a Named Tag using queries:

  • .NET
  • Java
  • Python
  • Node.js
try
{
  // Precondition: Cache is already connected

  // Items are already present in the cache with Named Tags

  // A user wants to end the VIP membership discount offer so this Named Tag needs to be removed from the customer
  // All the customers with VIP membership discount Named Tag need to be removed from the cache
  // Custom class is query indexed through the NCache Management Center or config.ncconf

  // Make sure to use the Fully Qualified Name (FQN)
  // Create an SQL Query with the specified criteria

  string query = "DELETE FROM FQN.Customer WHERE VIP_Membership_Discount = 0.12 ";

  // Use QueryCommand for query execution
  var queryCommand = new QueryCommand(query);

  // 'rowsAffected' indicates the number of items removed from the cache
  int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);

  if (rowsAffected==0)
      Console.WriteLine($"No VIP members removed");
}
catch (OperationFailedException ex)
{
    if (ex.ErrorCode == NCacheErrorCodes.INCORRECT_FORMAT)
    {
        // Make sure that the query format is correct
    }
    else
    {
        // Exception can occur due to:
        // Connection Failures 
        // Operation Timeout
        // Operation performed during state transfer    
    }
}
catch (Exception ex)
{
    // Any generic exception like ArgumentException, ArgumentNullException    
}      
try
{
  // Precondition: Cache is already connected

  // Items are already present in the cache with Named Tags

  // A user wants to end the VIP membership discount offer so this Named Tag needs to be removed from the customer
  // All the customers with VIP membership discount Named Tag need to be removed from the cache
  // Custom class is query indexed through the NCache Management Center or config.ncconf

  // Make sure to use the Fully Qualified Name (FQN)
  // Create an SQL Query to remove customers with VIP membership discount
  String query = "DELETE FROM FQN.Customer WHERE VIP_Membership_Discount = 0.12 ";

  // Use QueryCommand for query execution
  QueryCommand queryCommand = new QueryCommand(query);

  // 'rowsAffected' indicates the number of items removed from the cache
  int rowsAffected = cache.getSearchService().executeNonQuery(queryCommand);

  if (rowsAffected > 0)
  {
      System.out.println(rowsAffected + " VIP members removed from cache.");
  }
  else
  {
      System.out.println("No VIP members removed");
  }
}
catch (OperationFailedException ex)
{
    if (ex.getErrorCode() == NCacheErrorCodes.INCORRECT_FORMAT) {
        // Make sure your format is correct
    }
    else
    {
        // 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

    # Items are already present in the cache with Named Tags

    # A user wants to end the VIP membership discount offer so this Named Tag needs to be removed from the customer
    # All the customers with VIP membership discount Named Tag need to be removed from the cache
    # Custom class is query indexed through the NCache Management Center or config.ncconf
    # Delete data from cache with the specified criteria

    # Make sure to use the Fully Qualified Name(FQN)
    query = "DELETE FROM FQN.Customer WHERE VIP_Membership_Discount = ?"

    # Use QueryCommand for query execution
    query_command = ncache.QueryCommand(query)

    parameter = {"VIP_Membership_Discount": 0.12}
    query_command.set_parameters(parameter)

    search_service = cache.get_search_service()

    # 'rows_affected' number of items removed from cache
    rows_affected = search_service.execute_non_query(query_command)

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

  // Items are already present in the cache with Named Tags

  // A user wants to end the VIP membership discount offer so this Named Tag needs to be removed from the customer
  // All the customers with VIP membership discount Named Tag need to be removed from the cache
  // Custom class is query indexed through the NCache Management Center or config.ncconf

  // Make sure to use the Fully Qualified Name (FQN)
  // Create an SQL Query with the specified criteria
  var query = "DELETE FROM FQN.Customer WHERE VIP_Membership_Discount = ?";

  // Use QueryCommand for query execution
  var queryCommand = new ncache.QueryCommand(query);
  var parameters = new Map();
  parameters.set("VIP_Membership_Discount",0.12);

  queryCommand.setParameters(parameters);

  var searchService = await this.cache.getSearchService();

  // 'rowsAffected' number of items removed from cache
  var rowsAffected = await searchService.executeNonQuery(queryCommand);
}
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.

To get more details on object queries, please refer to the SQL Reference for NCache section.

Another approach to removing data with Named Tags is to fetch the data by querying it and then using the Remove method on the fetched data to remove the selected values. Using this approach, the items associated with a specific Named Tag are removed. However, it is not recommended as it will increase the required network calls which is not very efficient for the user. For more details on the remove method, please refer to the Remove Named Tags From Cache Data docs.

Additional Resources

NCache provides a sample application for Tags 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