Alachisoft.com
Download NCache Now!

Contact Us

Data Search and Query in NCache (Distributed Cache)

Although distributed cache is really useful for improving performance and scalability, the true benefit comes when you're able to store almost all types of data in it. This makes distributed cache an essential in-memory data store. However, many distributed caches are limited in the sense that they provide only a key/value based hash-table interface where the objects can only be searched based on this key thus largely limiting its usefulness. The real benefit is when you are able to search this data intelligently, instead of just key/value search.

NCache lets you perform search through intelligent approaches. It maintains indices of all searchable attributes and has its own efficient indexing mechanism which boosts the performance of data access.


[C#]

ArrayList keys = _cache.GetGroupKeys("group", "sub-group");
IDictionary dic = _cache.GetGroupData("group", "sub-group");


[Java]

Collection keys = _cache.getGroupKeys("group", "sub-group");
HashMap dic = _cache.getGroupData("group", "sub-group");

 

Fetching Items based on Tags

NCache provides you with the facility of specifying "tags". You can fasten one or more keywords with cache objects. Tag acts as a logical identifier to the cache items. Tags in NCache provide a many-to-many grouping where one tag can contain multiple cached items and one cached item can belong to multiple tags. The items tagged with the same identifier can be referenced collectively through this tag name, thus allowing for group actions on the cache items.


[C#]

Hashtable tagsHashtable = _cache.GetAllTags(tags);

Hashtable tagHashtable = _cache.GetByTag(tags[0]);

[Java]

HashMap tagsHashtable = _cache.getAllTags(tags);

HashMap tagHashtable = _cache.getByTag(tags[0]);


Using SQL like Object Query Language

NCache provides a SQL like Object Query Language (OQL) to let you search the distributed cache based on object attributes rather than the keys. NCache requires all searchable attributes to be indexed which helps quickly find the desired objects. The object query is distributed to all the cache servers to be run in parallel or just a single server depending on the topology being used and the results are then consolidated and returned. NCache lets you make API calls and specify a search based on this object query language in order to fetch a collection of objects from the cache.

The following code snippet allows you to search objects in your cache using OQL:


[C#]

String query = "SELECT Northwind.Business.Product WHERE this.ProductID > ?";

Hashtable param = new Hashtable();

param.Add("ProductID", 100);

// Fetch the keys matching this search criteria

ICollection keys = _cache.Search(query, param);

IEnumerator ie = keys.GetEnumerator();

while (ie.MoveNext()){

       String key = (String)ie.Current;

      Product prod = (Product)_cache.Get(key);

Console.WriteLine("ProductID: {0}", prod.ProductID);
}

[Java]

String query = "SELECT Northwind.Business.Product WHERE this.ProductID > ?"; 
Hashtable param = new Hashtable();
param.put(“ProductID”, 100);
// Fetch the keys matching this search criteria
Collection keys = _cache.search(query, param);

Iterator ide = keys.iterator();

            while (ide.hasNext()) {

       // do something here           

}

LINQ for .NET based Applications

LINQ for .NET helps you in unifying the way data is accessed from various sources (objects, database, XML etc). NCache allows you to run LINQ queries on your cached items by implementing a class named "NCacheQuery", which implements interface "IQueryable" provided by .NET. For executing LINQ queries, all that is required is to define an object implementing IQueryable interface and pass an instance of cache which contains the object.

NCache is integrated with LINQ in such a way that absolutely no code change is required in your existing LINQ queries. The following code snippet shows the same in action.

[C#]

using Alachisoft.NCache.Linq;

//...

IQueryable<Product> products = new NCacheQuery<Product>(_cache);

var result1 = from product in products

                     where product.ProductID > 10

                     select product;

if (result1 != null){

    foreach (Product p in result1) {

       Console.WriteLine("ProductID : " + p.ProductID);

    }

}

else {

        Console.WriteLine("No record found.");

}

Conclusion

NCache (Distributed Cache) provides a number of efficient and intelligent mechanisms for searching the cache for all types of data stored in it. NCache allows you to search through a huge amount of data in a simple and quicker way without degrading the cache performance.