Using DateTime Functions
NCache provides you with an ease of searching the cache by providing parameters in various formats. You can use various functions in a single query for faster cache searching.
You can use DateTime
in your query to search the cache with respect to a particular date or time. Make sure that you specify the date in the correct format. NCache supports various date formats, for more detail on these formats please refer to the DateTime Struct provided by Microsoft.
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 executes a query which searches for all the orders in the cache whose orderDate is lesser than the date specified using ExecuteReader.
try
{
// Pre-condition: Cache is already connected
// 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.Order WHERE this.OrderDate < ?";
// Create a new hashtable
Hashtable values = new Hashtable();
// Add the value of the OrderDate in the hastable
// You can use any format of the DateTime supported by NCache
string key = "OrderDate";
DateTime date = new DateTime(2017, 02, 01);
values.Add(key, date);
// Query the cached Data
ICacheReader reader = cache.ExecuteReader(query, values);
// Check if the result set is not empty
if (reader.FieldCount > 0)
{
while (reader.Read())
{
// Get the value of the result set
Order result = (Order)reader.GetValue(1);
// reader.GetValue(0) = Cache key
// reader.GetValue(1) = Product object
}
}
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
SQL Search for Objects
Using IN Operator
Using Like Operator
Using Group By
Query Operators
Search Cache with LINQ