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

Delete Group Data in Cache with SQL Query [Deprecated]

NCache provides object queries similar to SQL that allow you to delete cache items based on specified criteria, making it useful for cache cleanup and logical data partition management. To support this, NCache extends standard SQL syntax with the special $Group$ keyword, which enables you to target and delete all cache items belonging to a specific group or set of groups. The delete operation is executed using ExecuteNonQuery, which processes the query entirely on the server to minimize network overhead and returns the number of cache items removed. To use this approach, cached objects must be query-indexed and associated with groups in advance.

Prerequisites

Before deleting group data in cache using the SQL Query, 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, executeReader, 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.client.services import *
    • from ncache.runtime.caching 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, execute_reader, get_search_service, QueryCommand.
  • Install and include the following module in your Node.js client application:
    • Enterprise: ncache-client
  • Include the following class in your application:
    • Cache
  • 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, executeNonQuery, QueryCommand, CacheReader, getSearchService.

Delete Group

Given certain criteria, you can remove groups present in the cache with SQL queries. The following example removes all the data against a certain group through the SQL delete query.

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

  // A user wants to remove the West Coast Customers Group as this region is no longer supported in their store
  // Custom class is query indexed through the NCache Management Center or config.ncconf
  // Create SQL Query with the specified criteria
  // Make sure to use the Fully Qualified Name (FQN)
  string query = "DELETE FROM FQN.Customer WHERE $Group$ = ?";

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

  // Providing parameters for query
  queryCommand.Parameters.Add("$Group$", "West Coast Customers");

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

  if (rowsAffected == 0)
  {
      Console.WriteLine($"No West Coast Customers 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 other generic exception like ArgumentNullException or ArgumentException
}  
try
{
  // Precondition: Cache is already connected

  // A user wants to remove the West Coast Customers Group as this region is no longer supported in their store
  // Custom class is query indexed through the NCache Management Center or config.ncconf
  // Create SQL Query with the specified criteria
  // Make sure to use the Fully Qualified Name (FQN)
  String query = "DELETE FROM FQN.Customer WHERE $Group$ = ?";

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

  // Providing parameters for query
  queryCommand.getParameters().put("$Group$", "West Coast Customers");

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

  if (rowsAffected == 0)
  {
      System.out.println("No west coast customers removed");
  }
}
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 NullPointerException or IllegalArgumentException
}  
try:
    # Precondition: Cache is already connected

    # A user wants to remove the West Coast Customers Group as this region is no longer supported in their store
    # Custom class is query indexed through the NCache Management Center or config.ncconf
    # Create SQL Query with the specified criteria
    # Make sure to use the Fully Qualified Name (FQN)
    query = "DELETE FROM FQN.Customer WHERE $Group$ = ?"

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

    # Provide parameters for the query
    query_command.set_parameters({"$Group$": "West Coast Customers"})

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

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

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

  // A user wants to remove the West Coast Customers Group as this region is no longer supported in their store
  // Custom class is query indexed through the NCache Management Center or config.ncconf
  // Create SQL Query with the specified criteria
  // Make sure to use the Fully Qualified Name (FQN)
  const query = "DELETE FROM FQN.Customer WHERE $Group$ = ?";

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

  // Providing parameters for query
  const parameters = new Map();
  parameters.set("$Group$", "West Coast Customers");
  queryCommand.setParameters(parameters);

  // 'rowsAffected' number of items removed from cache
  const searchService = await cache.getSearchService();
  const rowsAffected = await searchService.executeNonQuery(queryCommand);

  if (rowsAffected === 0) 
  {
      console.log("No West Coast Customers removed");
  }
}
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.

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

Additional Resources

NCache provides sample application for Groups on GitHub.

See Also

.NET: Alachisoft.NCache.Client namespace.
Java: com.alachisoft.ncache.client namespace.
Python: ncache.client class.
Node.js: Cache 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