Alachisoft NCache 4.1 - Online Documentation

Queries

 
NOTE: This feature is not available in NCache Express and Professional edition.
 
Your application typically uses a cache as a Hashtable where everything is stored based on a key and you must have this key to fetch an item. This is like having a relational database where you can use primary key only to find data. This works fine in many situations but a real life complex application often needs to find data based on attributes other than just the primary key. And, since you're keeping a lot of your data in the cache, it would be very useful if you could search the cache in this manner as well. NCache provides this facility. NCache provides an Object Query Language (OQL) to let you search the cache. You have to make API calls and specify a search based on OQL in order to fetch collection of objects from the cache. NCache requires all searchable attributes to be indexed. See Indexing Searchable Attributes in .NET for details on indexing. Following API is available to search the cache:
 
public ICollection Search(string query, IDictionary parameters)
public IDictionary SearchEntries(string query, IDictionary parameters)
 
Query: Query string but instead of values '?' will be specified by user.
Parameters: Key-value pairs of attribute-names and their values. Both attribute-names and their values will be saved in string format.
 
 
Cache Search Returning Keys
 
The following code searches the cache and returns a collection of keys. Then, it iterates over all the returned keys and individually fetches the corresponding cached items from the cache.
Include the following namesapce in your application:
 
using Alachisoft.NCache.Web.Caching;
 
public class Program
{
public static void Main (string[] args)
{
Cache _cache = NCache.InitializeCache("myreplicatedcache");
String query = "SELECT NCacheQuerySample.Business.Product WHERE this.ProductID > ?";
Hashtable values = new Hashtable();
values.Add("ProductID",100);
// Fetch the keys matching this search criteria
ICollection keys = _cache.Search(query, values);
 
if (keys.Count > 0)
{
IEnumerator ie = keys.GetEnumerator();
while (ie.MoveNext())
{
String key = (String)ie.Current;
Product prod = (Product)NCache.Cache.Get(key);
HandleProduct(prod);
Console.WriteLine("ProductID: {0}", prod.ProductID);
}
}
_cache.Dispose();
}
}
 
The benefit of this approach is that the query returns keys only. Then, the client application can determine which key it wants to fetch. The drawback of this approach is that if you're going to fetch most of the items from the cache anyway, then you'll end up making a lot of trips to the cache. And, if the cache is distributed, this may eventually become costly.
 
Cache Search Returning Items
 
In some situations when you intend to fetch all or most of the cached items associated with cache keys, it is better to fetch all the keys and items in one call. Below is an example for this. It searches the cache and returns a dictionary containing both keys and values. This way, all the data based on the search criteria is fetched in one call to NCache. This is much more efficient way of fetching all the data from the cache.
Include the following namesapce in your application:
 
using Alachisoft.NCache.Web.Caching;
 
public class Program
{
public static void Main (string[] args)
{
Cache _cache = NCache.InitializeCache("myreplicatedcache");
String query = "SELECT NCacheQuerySample.Business.Product WHERE this.ProductID > ?";
Hashtable values = new Hashtable();
values.Add("ProductID", 100);
// Fetch the keys matching this search criteria
IDictionary dict = _cache.SearchEntries(query, values);
 
if (dict.Count > 0)
{
IDictionaryEnumerator ide = dict.GetEnumerator();
while (ide.MoveNext())
{
String key = (String)ide.Key;
Product prod = (Product)ide.Value;
HandleProduct(prod);
Console.WriteLine("Key = {0}, ProductID: {1}", key, prod.ProductID);
}
}
_cache.Dispose();
}
}
 
 
See Also

 
Copyright © 2005-2012 Alachisoft. All rights reserved.