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

How to Delete Cache Data for a Single Tag using SQL

NCache provides the ability to delete tagged data using SQL-like syntax via the ExecuteNonQuery method, allowing for efficient bulk removal in distributed environments. Object queries can also be used to remove items from the cache by using Tags as SQL query criteria. By using the $Tag$ keyword, NCache ensures that applications can identify and purge specific datasets without the overhead of manual iteration.

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, 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, CacheReader, 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, CacheReader, execute_non_query, QueryCommand getSearchService.
  • Install and include the following module in your Node.js client application:
    • Enterprise: ncache-client
  • Include the following class in your application:
    • Cache
    • Tag
  • 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, executeReader, executeNonQueryr, QueryCommand, CacheReader, get_search_service.

SQL DELETE for One Tag

Using object queries, items can be deleted using a single Tag. The following example deletes all the items associated with the Tag West Coast Customers using the SQL query.

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

  // Make sure to use the Fully Qualified Name (FQN)
  string query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";

  // Create query command and add parameter
  var queryCommand = new QueryCommand(query);
  queryCommand.Parameters.Add("$Tag$", "West Coast Customers");

  // Executing Query
  int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);

  if (rowsAffected == 0)
      Console.WriteLine($"No Customers from West Coast found");
}
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

  // Make sure to use the Fully Qualified Name (FQN)
  String query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";

  // Use QueryCommand for query execution
  QueryCommand queryCommand = new QueryCommand(query);
  queryCommand.getParameters().put("$Tag$", "West Coast Customers");

  // Execute the query
  int rowsAffected = cache.getSearchService().executeNonQuery(queryCommand);

  if (rowsAffected == 0) 
      System.out.println("No Customers from West Coast found");
}
catch (OperationFailedException ex)
{
    if (ex.getErrorCode() == 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 IllegalArgumentException or NullPointerException  
}
try:
    # Precondition: Cache is already connected

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

    # Create query command and add parameter
    query_command = ncache.QueryCommand(query)
    query_command.set_parameters({"$Tag$": "West Coast Customers"})

    search_service = cache.get_search_service()

    # Executing Query
    rows_affected = search_service.execute_non_query(query_command)

    if rows_affected == 0:
        print("No Customers from West Coast found")

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

  // Make sure to use the Fully Qualified Name (FQN)
  const query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";

  // Create query command and set parameter
  const queryCommand = new ncache.QueryCommand(query);
  const paramMap = new Map();
  paramMap.set("$Tag$", "VIP Customers");
  queryCommand.setParameters(paramMap);

  // Executing Query
  var searchService = await this.cache.getSearchService();
  var rowsAffected = await searchService.executeNonQuery(queryCommand);

  if (rowsAffected === 0)
      console.log(`No Customers from West Coast found`);
}
catch(error)
{
    // Handle errors
}    
Warning

Providing a Null Tag value for the query will throw an ArgumentNullException or NullPointerException.

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.

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: Tag 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