Search Cache Data with Named Tags and SQL
Note
This feature is only available in NCache Enterprise Edition.
Object queries can also be used to retrieve items from cache with your query criteria being Named Tags. The query when executed will search the cache according to the Named Tag provided in the SQL query. You can use the older/existing syntax or the Modern Syntax to carry out the search. Before carrying out the search, 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
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.
Modern Syntax
The following example retrieves the Products from the cache where the values of Named Tags match the provided value using the Modern Syntax.
Note
Use fully-qualified name of the class Product e.g. Data.Product
.
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
// Create SQL Query with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("FlashSaleDiscount", 0.5);
// 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
Product result = reader.GetValue<Product>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Product 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.
The following example retrieves the Products from the cache where the values of Named Tags match the provided value using the Legacy Syntax.
Note
Use fully-qualified name of the class Product e.g. Data.Product
.
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
// Search cache with the specified criteria
// Make sure to use the Fully Qualified Name (FQN)
string query = "SELECT FQN.Product WHERE this.FlashSaleDiscount = ?";
// Use QueryCommand for query execution
var queryCommand = new QueryCommand(query);
queryCommand.Parameters.Add("FlashSaleDiscount", 0.5);
// 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
Product result = reader.GetValue<Product>(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Product 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.
Warning
If you have multiple applications that are sharing the same cache and all of them are supposed to add Named Tags, then make sure that same Named Tags have homogenous data types. E.g. If one client is adding Named Tag ProductID with String data type then all other clients should add values of ProductID only in String format for same cache.
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:
Shipped with NCache: %NCHOME%\samples\dotnet\GroupsAndTags
See Also
Tag Cache Data
Data Expiration
Add Items with NamedTags
SQL Delete with NamedTags