Alachisoft NCache 3.8

Using Queries in Java

Feature is not available under 3.8.0.0

NCache allows you to fetch collection of objects from cache, in Java based applications. You have to make API calls and specify a search criteria based on Object Query Language to search the cache. NCache requires all searchable attributes to be indexed. For details on indexing, see Indexing Searchable attributes in Java.


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.


import com.alachisoft.ncache.exceptions.*;

import com.alachisoft.ncache.web.caching.*;

import java.util.Collection;

import java.util.Iterator;


public class QueryProgram {

   

    public static void main(String args[])

    {

        Cache c=null;

        try

        {

            c = NCache.initializeCache("mycache");

        }

        catch (CacheException ex)

        {

            ex.printStackTrace();

            return;

        }

 

        String query = "select javaapplication5.Product where this.ProductID > ?";

        Hashtable values = new Hashtable(); 

        values.Add("ProductID", 100); 

        try

        {

            // Fetch the keys matching this search criteria

            Collection coll = c.search(query, values);

            Iterator ps = coll.iterator();

           

            for(;ps.hasNext();)

            {

                System.out.println(ps.next());

            }

            c.dispose();

        }

        catch (CacheException ex)

        {

            ex.printStackTrace();

        }

    }

}


Cache Search Returning Items


In some situations, it is better to fetch all the keys and items in one call. Below is the example doing just that. It searches the cache and returns a dictionary containing both keys and values.


import com.alachisoft.ncache.exceptions.*;

import com.alachisoft.ncache.web.caching.*;

import java.util.Collection;

import java.util.Iterator;


public class QueryProgram {

   

    public static void main(String args[])

    {

        Cache c=null;

        try

        {

            c = NCache.initializeCache("mycache");

        }

        catch (CacheException ex)

        {

            ex.printStackTrace();

            return;

        }

        String query = "select javaapplication5.Product where this.ProductID > ?";

        Hashtable values = new Hashtable();

        values.Add("ProductID", 100); 

        try

        {

            // Fetch the keys matching this search criteria

            HashMap entries = c.searchEntries(query, values);

            for(Object key : entries.keySet())

            {

                Product prod = (Product)entries.get(key);

                System.out.println("key: " + (String)key + " -- " + "ProductId: " + prod.ProductID);

            }

            c.dispose();

        }

        catch (CacheException ex)

        {

            ex.printStackTrace();

        }

    }

}

     

search and searchEntries methods throw GeneralFailureException, OperationFailedException, AggregateException and SecurityException.


See Also

Object Query Language Syntax | Indexing Searchable attributes in Java