SQL Search for Objects
NCache lets you retrieve the whole item along with the keys using SQL queries. This is done using
ExecuteReader()
returns the result set in tabular form to ICacheReader
type
of instance. Column count and attribute values can be fetched through their
names or through index number from ICacheReader
. Make sure the instance of
ICacheReader
is always closed after execution, as it is necessary to clean the
resources.
Pre-Requisites
- Indexing for searchable objects and their attributes need to be configured first as explained in Configuring Query Indexes in Administrator's Guide.
- Include the following namespace in your application:
Alachisoft.NCache.Web.Caching
- The application must be connected to cache before performing the operation.
- Cache must be running.
- 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.
The following example creates a query which searches the cache according to the given criteria, executes it using ExecuteReader and then returns the values of the result set. The criteria being used is the UnitsInStock being greater than 10.
try
{
// Pre-condition: Cache is already connected
// Items are already present in the cache.
// Create a query which will be executed on the data set
// Use the fully qualified name of your own custom class
string query = "SELECT Alachisoft.NCache.Sample.Data.Product WHERE this.UnitsInStock > ?";
// Create a new hashtable
Hashtable values = new Hashtable();
// Add the values of the UnitsInStock in the hashtable
values.Add("UnitsInStock", 10);
// Query Cached Data
ICacheReader reader = cache.ExecuteReader(query, values);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
Product result = (Product)reader.GetValue(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Product object
// Perform operations
}
}
else
{
// Null query result set retrieved
}
}
catch (OperationFailedException ex)
{
if (ex.Message.Contains("Incorrect query format"))
{
// Make sure that the query format is correct
}
else if (ex.Message.Contains("Index is not defined"))
{
// Make sure that index is defined using Query Indexes through NCache Manager
}
else
{
// Exception can occur due to:
// Connection Failures
// Operation performed during state transfer
// Operation Timeout
}
}
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.
Additional Resources
NCache provides sample application for SQL Searching at:
Shipped with NCache: %NCHOME%\samples\dotnet\ObjectQueryLanguage
See Also
Using Locks for Concurrent Updates
SQL Search for Keys
Using IN Operator
Using Like Operator
Using Group By
Query Operators
Search Cache with LINQ