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.
Prerequisites
- Install the following NuGet package in your application:
- Include the following namespace in your application:
Alachisoft.NCache.Client
Alachisoft.NCache.Runtime.Caching
Alachisoft.NCache.Runtime.Exceptions
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: ICache.
- 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.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Import the following packages in your application:
import com.alachisoft.ncache.client.*;
import com.alachisoft.ncache.runtime.exceptions.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: Cache.
- 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.
- Install and include the following module in your application:
- Enterprise:
const ncache = require('ncache-client')
- Cache must be running.
- The application must be connected to cache before performing the operation.
- For API details, refer to: Cache.
- 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.
- Install the NCache Python client by executing the following command:
# Enterprise Client
pip install ncache-client
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Import the NCache module in your application.
- Cache must be running.
- 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.
- Add the following Maven dependencies in your
pom.xml
file:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<artifactId>ncache-scala-client</artifactId>
<version>x.x.x</version>
</dependency>
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Import the following packages in your application:
import com.alachisoft.ncache.scala.client.*;
- Cache must be running.
- The application must be connected to cache before performing the operation.
- 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
}
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
QueryCommand queryCommand = new QueryCommand(query);
queryCommand.getParameters().put("FlashSaleDiscount", 0.5);
// Executing query
CacheReader reader = cache.getSearchService().executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
Product result = reader.getValue(1,Product.class);
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.getErrorCode() == 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 IllegalArgumentException or NullPointerException
}
// This is an async method
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)
var query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?";
// Use QueryCommand for query execution
var queryCommand = new ncache.QueryCommand(query);
let parameter = new Map();
parameter.set("FlashSaleDiscount", 0.5);
queryCommand.setParameters(parameter);
// Executing query
var searchService = await this.cache.getSearchService();
var reader = await searchService.executeReader(queryCommand);
// Check if result set is not empty
if (reader.getFieldCount() > 0)
{
while (reader.read())
{
// Get the value of the result set
var result = reader.getValue(1, ncache.JsonDataType.Object);
}
}
else
{
// Null query result set retrieved
}
} catch (error)
{
// Handle errors
}
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)
query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?"
# Use QueryCommand for query execution
query_command = ncache.QueryCommand(query)
parameter = {"FlashSaleDiscount": 0.5}
query_command.set_parameters(parameter)
# Executing query
search_service = cache.get_search_service()
reader = search_service.execute_reader(query_command)
# Check if result set is not empty
if reader.get_field_count() > 0:
while reader.read():
# Get the value of the result set
result = reader.get_value(Product, 1)
else:
# None query result set retrieved
print("Query result is None")
except Exception as exp:
# Handle errors
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)
val query = "SELECT $Value$ FROM FQN.Product WHERE FlashSaleDiscount = ?"
// Use QueryCommand for query execution
val queryCommand = new QueryCommand(query)
queryCommand.setParameters(Map("FlashSaleDiscount" -> 0.5))
// Executing query
val reader = cache.getSearchService.executeReader(queryCommand)
// Check if result set is not empty
if (reader.getFieldCount > 0) while ( {
reader.read
}) { // Get the value of the result set
val result = reader.getValue(1, classOf[Product])
}
else {
// Null query result set retrieved
}
}
catch {
case exception: Exception => {
// Handle any errors
}
}
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