Getting Objects from Cache

Getting Object from Cache in Three Lines

Call method Cache.get() to put an object to the cache:

Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache = cacheonix.getCache("my.cache");
String cachedValue = cache.get("my.key");

Identifying if Key Is in Cache

Cache.get() will return null if the object is not in cache, or if the object is null. To distinguish between these two situations, Cacheonix provides method Cache.exist() that allows to check whether a keys exists in the cache:

Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache = cacheonix.getCache("my.cache");
boolean keyExists = cache.keyExists("my.key");
if (keyExists) {
   . . .
}

Getting Collection of Objects from Cache

Cacheonix provides method Cache.getAll() that allows to retrieve a collection of values associated with a set of keys:

// Get cache
Cacheonix cacheonix = Cacheonix.getInstance();
Cache<String, String> cache= cacheonix.getCache("my.cache");

// Create a key set
Set<String> keySet = new HashSet(2);
keySet.add("my.key.0");
keySet.add("my.key.0");

// Get values associated with the key set
Collection<String> cachedValues = cache.getAll(keySet);

See also:

Labels

 
(None)