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
- 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.
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.
Modern Syntax
Following example deletes all the items associated with the tag Beverages using the SQL query with the Modern Syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
Note
Use fully-qualified name of the class Customer e.g. Data.Customer
.
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");
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.
Following example deletes all the items associated with the tag Beverages using the SQL query with the older syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
Note
Use fully-qualified name of the class Customer e.g. Data.Customer
.
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 FQN.Customer WHERE this.$Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "Important Customers");
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.
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. You can carry this SQL delete using older or Modern Syntax.
Modern Syntax
Following example removes all the items associated with any of the tags from the list using the SQL query with the Modern Syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
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);
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.
Following example removes all the items associated with any of the tags from the list using the SQL query with the Legacy Syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
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 FQN.Customer WHERE this.$Tag$ = ? OR this.$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);
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.
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 using the older syntax or the Modern Syntax.
Modern Syntax
Following example removes all the items associated with any of the tags from the list using the SQL query with the Modern Syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
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);
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.
Following example removes all the items associated with any of the tags from the list using the SQL query with the Legacy Syntax.
Warning
Providing Null
tag value for the query will throw an ArgumentNullException.
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 your custom class
string query = "DELETE FQN.Customer WHERE this.$Tag$ = ? AND this.$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);
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.
Additional Resources
NCache provides sample application for Tags at:
- GitHub
- Shipped with NCache: %NCHOME%\samples\dotnet\GroupsAndTags
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