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
- Install the following NuGet packages:
- 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.
Syntax
An example below shows how to delete items associated with a Named Tag using queries.
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
}
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