Thursday, September 06, 2007

Coherence Contd.. Querying Cache

One of the features of Coherence is that it provides a "Queryeable" data structure. So if you followed my last blog you already know how to populate data in a Cache. A Cache is typically like a Database table and only one "type" of values should be stored. Even though a Cartesan product can be implemented (Storing two types of data in the same table) but should be avoided. Assuming the Cache service is running and has data in it, the following class shows how to use Filters to make a Query. It is important that the Cache does have data in it and not expired before the query can be run against it.

package com.ezduck.coherence;


import com.tangosol.util.Filter;
import com.tangosol.util.filter.LikeFilter;

import java.util.Map;
import java.util.Set;
import java.util.Iterator;

public class QueryCache {

private static NamedCache nCache;

static {
nCache = CacheFactory.getCache("Name");

public QueryCache() {

public static void main(String[] args) {
QueryCache queryCache = new QueryCache();
// -- Look for Names which starts with A
Filter filter = new LikeFilter ("getName", "A%");
Set set = nCache.entrySet(filter);

Iterator iter = set.iterator();
while (iter.hasNext()) {
System.out.println ("Val: " + ((Map.Entry);


And Thats it!

No comments: