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. 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
- Install the following NuGet packages:
- 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.
- For API details, refer to: ICache.
- 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.
- To handle any unseen exceptions, refer to the Troubleshooting section.
Syntax
The following example retrieves the Products from the cache where the values of Named Tags match the provided value.
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);
}
}
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 on GitHub.
See Also
Tag Cache Data
Data Expiration
Add Items with NamedTags
SQL Delete with NamedTags