|
Back to JOC 10.1.2 Tutorial Index
Disk Cache
The Java Object Cache can manage objects on disk as well as in memory. Like memory objects, disk objects can be local or distributed. Unlike memory objects, distributed disk objects are shared by all caches that have access to the file system hosting the disk cache. This allows for better utilization of disk resources and allows disk objects to survive beyond the life of the server process. Objects on disk are identified by absolute path names so all caches that share a disk cache must have the same file directory structure. Local disk objects are only visible to the cache that loaded them so they will not survive process termination.
Objects can be added to the disk cache in various ways. They may be memory objects that have been spooled to disk to free space in the memory cache, StreamAccess objects (see the chapter on Stream Access Objects for more details), objects that are explicitly saved to disk via CacheAccess.save(), or objects that live exclusively on disk.
When space is running out in the memory cache, the Cache Service will search through the cache looking for objects that are not currently being accessed. These objects will be removed from the cache. If the object was expensive to create you may want the object written to disk to avoid having to recreate it later. This can be done by setting the SPOOL attribute flag (see the chapter on Cache Object Attributes for details).
In some situations you may want to force one or more objects to be written to disk. This can be done using the CacheAccess.save() method. save will synchronously write the object to disk if the object is not already on disk. If save is called on a group or region all objects within the group or region will be written to disk. If an object is encountered that cannot be written to disk (it is not Serializable, for example), the event will be recorded in the log and the save operation will continue.
Objects that are accessed directly from disk are loaded into the disk cache by calling CacheLoader.createDiskObject() from the CacheLoader.load() method (see Automatic Loading for details). createDiskObject returns a File object that can be used to load the disk cache object. When CacheAccess.get() is called, the full path name to the file is returned so the application can open the file in the manner appropriate to the application. If the attributes have not already been defined for the object, they should be set with the createDiskObject method. This will ensure the object is managed correctly on disk.
The following is an example of a loader object to load a disk object into the cache.
import oracle.ias.cache.*;
class YourObjectLoader extends CacheLoader
{
public Object load(Object handle, Object args)
{
File file;
FileOutputStream = out;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
try
{
file = createDiskObject(handle, attr);
out = new FileOutputStream(file);
out.write((byte[])getInfoFromSomewhere());
out.close();
}
catch (Exception ex)
{
// translate exception to CacheException, and log exception
throw exception("exception in file handling", ex)
}
return file;
}
}
The application code would be something like the segment below. This assumes the "Stock-Market" region has already been defined with a YourObjectLoader loader as the default loader for the region.
import oracle.ias.cache.*;
try
{
FileInputStream in;
File file;
String filePath;
CacheAccess cacc = CacheAccess.getAccess("Stock-Market");
filePath = (String)cacc.get("file object");
file = new File(filePath);
in = new FileInputStream(filePath);
in.read(buf);
// do something interesting with the data
in.close();
cacc.close();
}
catch (Exception ex)
{
// handle exception
}
Back to JOC 10.1.2 Tutorial Index
|