NCache supports SQL-like language called Object Query Language (OQL). Syntax of the language is explained with the help of examples below:
Using Equal Operator:
Cache _cache = NCache.initializeCache( "Cache" );
String queryString = "select NCacheQuerySample.Business.Product where this.Category = ?" ;
Hashtable searchCriteria = new Hashtable();
searchCriteria.put( "ProductID" , 10 );
Collection keys = _cache.search(queryString, searchCriteria);// Return Keys
Dictionary dict = _cache.SearchEntries(query, searchCriteria);// Return Keys and values
Using Multiple Operators:
Cache _cache = NCache.initializeCache( "Cache" );
String queryString = "select NCacheQuerySample.Business.Product where this.ProductID < ? AND this.Category = ?" ;
Hashtable searchCriteria = new Hashtable();
searchCriteria.put( "ProductID" , 10 );
searchCriteria.put( "Category" , 4 );
Collection keys = _cache.search(queryString, searchCriteria);// Return Keys
Dictionary dict = _cache.SearchEntries(query, searchCriteria);// Return Keys and values
Using IN Operator:
Cache _cache = NCache.initializeCache( "Cache" );
String queryString = "select NCacheQuerySample.Business.Product where this.ProductID IN (?,?,?)" ;
ArrayList aList = new ArrayList();
aList.add( 10 );
aList.add( 4 );
aList.add( 7 );
Hashtable searchCriteria = new Hashtable();
searchCriteria.put( "ProductID" , aList);
Collection keys = _cache.search(queryString, searchCriteria);// Return Keys
Dictionary dict = _cache.SearchEntries(query, searchCriteria);// Return Keys and values
Using LIKE Operator:
Cache _cache = NCache.initializeCache( "Cache" );
String queryString = "select NCacheQuerySample.Business.Product where this.ProductName LIKE ?" ;
ArrayList aList = new ArrayList();
aList.add( "Ch " ); //returns all entries that start with "Ch".
Hashtable searchCriteria = new Hashtable();
searchCriteria.put( "ProductName" , aList);
Collection keys = _cache.search(queryString, searchCriteria);// Return Keys
Dictionary dict = _cache.SearchEntries(query, searchCriteria);// Return Keys and values
NCache provides you Tagging facility. You can fasten one or more keywords with cache objects. Items can be added to the cache having multiple tags. Tag acts as an identifying marker to the cache items.
So one can get items on the basis of Tags. Tags are Indexed in NCache thus giving user the ability to search for Tagged cache objects using Queries. Please be aware that Query strings should be specific as shown in the following example
Cache _cache = NCache.initializeCache( "myCache" );
Object valueToStore = "Can contain any type" ;
Tag[] tagList = new Tag[ 2 ];
tagList[ 0 ] = new Tag( "myTag1" );
tagList[ 1 ] = new Tag( "myTag2" );
// Add items to cache containing tags
_cache.add( "x-y-z" , valueToStore, tagList);
Hashtable searchCriteria = new Hashtable();
ArrayList queryTagList = new ArrayList();
queryTagList.add( "myTag1" );
queryTagList.add( "myTag2" );
searchCriteria.put( "$Tag$" , queryTagList);
// Query string, for searching Cache items with respect to Tags, should be specific to the following
_cache.search( "SELECT System.String WHERE this.$Tag$=? AND this.$Tag$=?" , searchCriteria);