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

Delete Cache Data with Named Tags and SQL

Note

This feature is only available in NCache Enterprise Edition.

NCache provides Object Queries in order to delete the particular item from the cache having the Named Tag and its value as provided. Make sure that item is added in the cache with the Named Tags. Refer to the Add Items with Named Tags section to get a detail on creating and adding Named Tags.

Pre-Requisites

  • .NET/.NET Core
  • Java
  • Node.js
  • Install the following NuGet packages:
    • Alachisoft.NCache.SDK.
  • Include the following namespace in your application:
    • Alachisoft.NCache.Client
    • Alachisoft.NCache.Runtime.Caching
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: ICache, Remove.
  • Custom classes/searchable attributes must be indexed as explained in Configuring Query Indexes.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Add the following Maven dependencies in your pom.xml file:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <artifactId>ncache-client</artifactId>
    <version>5.2.0</version>
</dependency>
  • Import the following packages in your application:
    • import com.alachisoft.ncache.client.*;
    • import com.alachisoft.ncache.runtime.exceptions.*;
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: Cache, remove.
  • Make sure that the data being added is serializable.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.
  • Include the following modules in your application
    • const ncache = require('ncache-client')
  • The application must be connected to cache before performing the operation.
  • Cache must be running.
  • For API details, refer to: Cache, remove.
  • To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.
  • To handle any unseen exceptions, refer to the Troubleshooting section.

Syntax

An example below shows how to delete items associated with a Named Tag using queries.

  • .NET/.NET Core
  • Java
  • Node.js
try
{
    // Pre-conditions: Cache is already connected
    // Items are already present in the cache with named tags
    // Custom class is query indexed through NCache Web Manager or config.ncconf
    // Delete data from cache with the specified criteria
    // Make sure to use the Fully Qualified Name (FQN)
    string query = "DELETE FROM FQN.Product WHERE FlashSaleDiscount = ?";

    // Use QueryCommand for query execution
    var queryCommand = new QueryCommand(query);
    queryCommand.Parameters.Add("FlashSaleDiscount", 0.5);

    // 'rowsAffected' number of items removed from cache
    int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);
}
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
{
    // Pre-conditions: Cache is already connected
    // Items are already present in the cache with named tags
    // Custom class is query indexed through NCache Web Manager or config.ncconf
    // Delete data from cache with the specified criteria
    // Make sure to use the Fully Qualified Name (FQN)
    String query = "DELETE FROM FQN.Product WHERE FlashSaleDiscount = ?";

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

    queryCommand.getParameters().put("FlashSaleDiscount", 0.5);

     // 'rowsAffected' number of items removed from cache
    int rowsAffected = cache.getSearchService().executeNonQuery(queryCommand);
}
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 
}
// This is an async method
try
{
    // Pre-conditions: Cache is already connected
    // Items are already present in the cache with named tags
    // Custom class is query indexed through NCache Web Manager or config.ncconf
    // Delete data from cache with the specified criteria
    // Make sure to use the Fully Qualified Name (FQN)
    var query = "DELETE FROM FQN.Product WHERE FlashSaleDiscount = ?";

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

    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
}

Recommendation: To ensure the operation is fail safe, it is recommended to handle any potential exceptions within your application, as explained in Handling Failures.

In order to get more detail on object queries please refer to the SQL Reference for NCache section.

Another approach that can be used in order to remove 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 a recommended approach as it will increase the network calls which is not very efficient for the user.

Additional Resources

NCache provides sample application for Tags on GitHub.

See Also

SQL Query with Named Tags
Remove Named Tags
Add Cache Data with Named Tags
Tag Cache Data

Back to top Copyright © 2017 Alachisoft