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.The delete operation can be carried out using the old and Modern Syntax. 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
- 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.
- 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.
Note
Use fully-qualified name of the class Product e.g. Data.Product
.
Modern Syntax
An example below shows how to delete items associated with a Named Tag using queries with the Modern Syntax.
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);
int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);
// 'rowsAffected' number of items were removed from cache
}
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.
Legacy Syntax
Note
Legacy API is only available in NCache Enterprise Edition.
An example below shows how to delete items associated with a Named Tag using queries with the Legacy syntax.
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 FQN.Product WHERE this.FlashSaleDiscount = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("FlashSaleDiscount", 0.5);
int rowsAffected = cache.SearchService.ExecuteNonQuery(queryCommand);
// 'rowsAffected' number of items were removed from cache
}
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 at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\GroupsAndTags
See Also
SQL Query with Named Tags
Remove Named Tags
Add Cache Data with Named Tags
Tag Cache Data