Delete Tag Data from Cache with SQL
Note
This feature is only available in NCache Enterprise Edition.
Similar to get, object queries can also be used to remove items from cache with your query criteria being tags. Object queries let you search the data in your cache and then delete it.
The idea of a special keyword $Tag$
also exists here.
ExecuteNonQuery
is used in queries containing delete operations. It returns the number of affected rows after query is executed.
Therefore, the three strategies of removing items on tags (i.e. by a specific tag, any tag or all tags) can also be adopted for these queries.
Pre-Requisites
- Install the following NuGet package in your application.
- 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.
- For API details refer to: ICache, ICacheReader, QueryCommand, ExecuteNonQuery
- 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.
SQL DELETE for One Tag
Using object queries, items can be deleted using a single tag. You can SQL delete for a single tag using the older syntax or the new one.
Syntax
Following example deletes all the items associated with the tag Beverages using the SQL query.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException or NullPointerException.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Web Manager or config.ncconf
// Make sure to use the Fully Qualified Name (FQN)
string query = "DELETE FROM FQN.Customer WHERE $Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "Important Customers");
// rowsAffected is the 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.
SQL DELETE for ANY Tag
Using object queries, if multiple tags are provided in the form of a list, items associated with ANY of the tags from the list are removed.
Syntax
Following example removes all the items associated with any of the tags from the list using the SQL query.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException or NullPointerException.
Note
Use fully-qualified name of the class Customer e.g. Data.Customer
.
try
{
// Pre-conditions: Cache is already connected
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Web Manager or config.ncconf
// Make sure to use the Fully Qualified Name (FQN)
string query = "DELETE FROM FQN.Customer WHERE $Tag$ = ? OR $Tag$ = ?";
var queryTagList = new ArrayList();
queryTagList.Add("Important Customers");
queryTagList.Add("East Coast Customers");
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", queryTagList);
// rowsAffected is the 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.
SQL DELETE for ALL Tags
Using object queries, if multiple tags are provided in the form of a list, items associated with ALL of the tags from the list are removed. You can carry out SQL delete for all tags.
Syntax
Following example removes all the items associated with any of the tags from the list using the SQL query.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException or NullPointerException.
Note
Use fully-qualified name of the class Customer e.g. Data.Customer
.
try
{
// Pre-conditions: Cache is already connected
// Items are already present in the cache with tags
// Custom class is query indexed through NCache Web Manager or config.ncconf
// Make sure to use the Fully Qualified Name (FQN) of custom class
string query = "DELETE FROM FQN.Customer WHERE $Tag$ = ? AND $Tag$ = ?";
var queryTagList = new ArrayList();
queryTagList.Add("Important Customers");
queryTagList.Add("East Coast Customers");
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", queryTagList);
// rowsAffected is the 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.
Additional Resources
NCache provides sample application for Tags on GitHub.
See Also
Using Groups
Add/Update Data in Cache with Tags
Retrieve Cache Data with Tags
Remove Cache Data with Tags
Search Tag Data in Cache with SQL
Named Tags