Search Tag Data in Cache with SQL
Note
This feature is only available in NCache Enterprise Edition.
NCache provides object queries through which you can search and delete result sets based on the criteria given to the query. To retrieve the data according to your specified criteria, NCache provides you with an extension of SQL. It lets you search the data in your cache based on the criteria on which requirement is based.
A special keyword $Tag$
is used to specify that the condition under consideration uses tags. A query with the searching criteria is executed using ExecuteReader.
ExecuteReader
processes the query on the server side and then sends the result in chunks (as a dictionary containing keys and values) to the client in the tabular form to ICacheReader
type of instance.
Similar to the retrieve and delete method,the three strategies of querying items on tags (i.e. by a specific tag, any tag or all tags) can also be adopted for these queries which are explained below.
Pre-Requisites
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Exceptions
- 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.
Search Data in Cache with One Tag
Using object queries, a single tag can be used to retrieve all the items associated with that tag.
Modern Syntax
Following example retrieves 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-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
// Search for items with tags
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "Select $Value$ FROM FQN.Customer WHERE $Tag$ = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "Important Customers");
// Executing query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
//reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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
}
Legacy Syntax
Note
Legacy API is only available in NCache Enterprise Edition.
Following example retrieves all the items associated with the tag Beverages 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
// Search for items with tags
// Use the Fully Qualified Name (FQN) of your own custom class
string query = "SELECT FQN.Customer WHERE this.$Tag$=?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", "Important Customers");
// Executing query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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.
Search Data in Cache with 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 retrieved.
Modern Syntax
Following example retrieves 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
// Finding items associated with any of the tags
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT $Value$ FROM FQN.Customer WHERE $Tag$ IN (?,?,?)";
// If multiple tags are used for query criteria, add tags in an array list
var queryTagList = new ArrayList();
queryTagList.Add("Important Customers");
queryTagList.Add("East Coast Customers");
queryTagList.Add("West Coast Customers");
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", queryTagList);
// Executing query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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 retrieves all the items associated with any of the tags from the list 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-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
// Finding items associated with any of the tags
// Make sure to use the Fully Qualified Name (FQN) of your custom class
string query = "SELECT FQN.Customer WHERE this.$Tag$ = ? OR this.$Tag$ = ?";
//OR
string query = "SELECT FQN.Customer WHERE this.$Tag$ IN (?,?,?)";
// If multiple tags are used for query criteria, add tags in an array list
var queryTagList = new ArrayList();
queryTagList.Add("Important Customers");
queryTagList.Add("East Coast Customers");
queryTagList.Add("West Coast Customers");
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("$Tag$", queryTagList);
// Executing query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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.
Search Data in Cache with 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 retrieved. It means that an item is not retrieved unless it contains all the tags in the list.
Modern Syntax
Following example retrieves all the items associated with all 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
// Finding items associated with any of the tags
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT $Value$ FROM FQN.Customer WHERE $Tag$ = ? AND $Tag$ = ?";
// If multiple tags are used for query criteria, add tags in an array list
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);
// Execute query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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
}
Legacy Syntax
Note
Legacy API is only available in NCache Enterprise Edition.
Following example retrieves all the items associated with all 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
// Finding items associated with any of the tags
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT FQN.Customer WHERE this.$Tag$=? AND this.$Tag$=?";
// If multiple tags are used for query criteria, add tags in an array list
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);
// Execute query
ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand);
// Check if result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Customer result = reader.GetValue<Customer>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Customer object
}
}
else
{
// Null query result set retrieved
}
}
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 NCache 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
Delete Tag Data from Cache with SQL
Named Tags