|
Back to JOC 10.1.2 Tutorial Index
Implementing a CacheLoader
To take advantage of automatic loading you must provide a CacheLoader object and implement your own load method. There are a number of methods available in the CacheLoader class for use within the user implemented load method. setAttributes sets the attributes for the object being loaded, netSearch can be used to check other caches in the system to see if the object exists elsewhere before loading it, getRegion returns the name of the current region, getName returns the name of the object being loaded, createStream creates a stream access object and returns an OutputStream object to be used to load the object, createDiskObject creates a file on disk for managing a disk cache object. exceptionHandler is available to process exceptions that may occur in the load method, and log allows logging into the cache log file from the load method.
Suppose you want to implement a loader that would first check other caches for the object then if it is not found, retrieve data from the database. The result is then stored in the cache as a String. The idle time attribute for the object is set to 60 seconds using the setAttributes method.
import oracle.ias.cache.*;
class YourObjectLoader extends CacheLoader
{
public YourObjectLoader()
{
}
public Object load(Object handle, Object args) throws CacheException
{
String contents;
Attributes attr = new Attributes();
attr.setTimeToLive(60); // set time to live to 60 seconds
// check if this object is loaded in another cache
try
{
contents = (String)netSearch(handle, 5000);
setAttributes(handle, attr);
return contents;
}
catch (Exception ex)
{}
try
{
contents = expensiveCall(args);
setAttributes(handle, attr);
return contents;
}
catch (Exception ex)
{
// translate exception to CacheException, and log exception
throw exception("exception in expensiveCall", ex)
}
}
private String expensiveCall(Object args)
{
String str = null;
// your implementation to retrieve data from the database
// str = ...
return str;
}
}
If attributes are being set for the object, it is best to set them within the load method. This will ensure they are properly set when the object is made available by the cache and will avoid the possibility of multiple threads setting the attributes concurrently.
Accessing Cache Objects from the Loader
At times it may be useful to load an object into the cache using other objects from the cache. The Java Object Cache is designed to allow this. The only restriction is the load method cannot try to access the object being loaded or the circular dependency will result in deadlock.
import oracle.ias.cache.*;
class YourObjectLoader extends CacheLoader
{
public YourObjectLoader()
{
}
public Object load(Object handle, Object args) throws CacheException
{
CacheAccess cacc;
Object cachedObject;
// find the region name
String region = getRegion(handle);
//get a CacheAccess handle and retrieve the cached object
cacc = CacheAccess.getAccess(region);
cachedObject = cacc.get("raw data");
// create the new cached object
String contents = processData(cachedObject);
cacc.close();
return contents;
}
private String processData(Object args)
{
String str = null;
Attributes attr = new Attributes();
attr.setTimeToLive(60); // set time to live to 60 seconds
// your implementation to process data
// str = ...
setAttributes(handle, attr);
return str;
}
}
Back to JOC 10.1.2 Tutorial Index
|