Delete Group Data in Cache with SQL
Note
This feature is only available in NCache Enterprise Edition.
Similar to searching with SQL, NCache provides object queries through which you use a criteria given in a query delete result sets. To delete the data in cache according to your specified criteria, NCache provides you with an extension of SQL.
A special keyword $Group$
is used to specify that the condition under consideration uses group. A query with the searching criteria is executed using ExecuteNonQuery
.
ExecuteNonQuery
processes the query on the server side and then sends the result to the client in the tabular form to ICacheReader
type of instance.
Pre-Requisites
- Install the following NuGet package in your application.
- Include the following namespaces in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime
- 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.
Delete Group
Given a certain criteria, you can search for groups present in the cache with SQL queries. The following program searches for a group against a certain Customer ID through SQL query i.e. the group that the Customer ID belongs to.
try
{
// Pre-conditions: Cache is already connected
// Custom class is query indexed through NCache Web Manager or config.ncconf
// Create SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
string query = "DELETE FROM FQN.Customer WHERE $Group$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Group$","ImportantCustomers");
// 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 other generic exception like ArgumentNullException or ArgumentException
}
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 Groups on GitHub.
See Also
Add/Update Cache Data with Groups
Retrieve Cache Data with Groups
Remove Cache Data with Group
Use Groups for Logical Data Grouping
Search Group Data in Cache with SQL