|
Back to JOC 10.1.2 Tutorial Index
Automatic Loading
For most objects, it is better to let the cache service load the object as needed rather than having the application put it in directly. This allows the cache to better manage the loading of objects and will keep the application code cleaner by making the "if not in cache then load" logic unnecessary. The loader will be invoked any time the CacheAccess.get() or CacheAccess.preLoad() methods are called and the object is not already in the cache. To use automatic loading, the application writer must implement a CacheLoader object. See the chapter on Implementing a CacheLoader for details. The loader object must be defined as an attribute of the object, or group or region the object is associated with. See the chapter on Defining Your Environment for details. A loader defined for a group or region is taken to be the "default" loader for all objects associated with the region or group. A loader defined for an individual object is only used to load that particular object. The same loader object may be defined for more than one cache object. Loader objects that are defined for a region, group or for more than one cache object need to be written with concurrent access in mind as the object will be shared.
import oracle.ias.cache.*;
try
{
CacheAccess cacc = CacheAccess.getAccess(region);
MyCacheLoader loader = new MyCacheLoader();
Attributes attr = new Attributes();
attr.setLoader(loader);
cacc.defineRegion("region name", attr); // define the loader for the whole region
// OR
cacc.defineObject("myObject", attr); // define the loader for an individual object
If the object is not in the cache, the get method will trigger a call to the defined loader passing in the args object as an argument to the loader. If the object is in the cache or is being loaded by another thread, the loader is not called.
// get is called with an args parameter to be passed to the loader if necessary
MyObject obj = (MyObject)cacc.get("myObject", args);
// OR
// get is called without an args parameter
MyObject obj = (MyObject)cacc.get("myObject");
cacc.close(); // release the CacheAccess object
}
catch (CacheException ex)
{
// handle exception
}
Back to JOC 10.1.2 Tutorial Index
|