Oracle9i Application Server Oracle9iAS Object Caching Service for Java Developer's Guide Release 1 (v1.0.2.2) Part Number A88852-01 |
|
This chapter shows you how to work with the disk cache for Object Caching Service for Java, and includes descriptions of disk objects and StreamAccess objects. Disk objects are objects that are stored on a local disk in a file, and the application using the Object Caching Service for Java accesses the file directly. StreamAccess objects are objects that are accessed as a stream (loaded as an OutputStream
and read as an InputStream)
.
This chapter covers the following topics:
The Object Caching Service for Java can manage objects on disk as well as in memory.
This section covers the following topics:
To configure the Object Caching Service for Java to use a disk cache, set the value of the diskPath
configuration property in the OCS4J.properties
file.
To configure the Object Caching Service for Java to use a disk cache, the
diskPath
property in the configuration properties file should be set to the path of the root directory for the disk cache. The default value for diskPath
is null, which specifies that the Object Caching Service for Java should not enable the disk cache.
This section covers the following topics:
When operating in local mode, all objects are treated as local objects (even when the DISTRIBUTE
attribute is set for an object). In local mode, all objects in the disk cache are only visible to the Object Caching Service for Java cache that loaded them, and they do not survive after process termination. In local mode, objects stored in the disk cache are lost when the process using the cache dies.
When operating in distributed mode, disk cache objects are shared by all caches that have access to the file system hosting the disk cache. This configuration allows for better utilization of disk resources and allows disk objects to persist beyond the life of the Object Caching Service for Java process. Distributed memory objects are not shared by all caches since individual copies of each memory object reside in the individual caches across the system.
Objects stored in the disk cache are identified using the concatenation of the path specified in the diskPath
configuration property and an internally generated String
representing the remaining path to the file. Thus, caches that share a disk cache can have a different directory structure, as long as the diskPath
represents the same directory on the physical disk and is accessible to the Object Caching Service for Java processes.
If a memory object that is saved to disk is also distributed, the memory object can survive the death of the process that spooled it.
See Also:
"Automatically Adding Objects" for information on using the |
There are several ways to use the disk cache with the Object Caching Service for Java, including:
The Object Caching Service for Java automatically adds certain objects to the disk cache. Such objects may reside either in the memory cache or in the disk cache. If an object in the disk cache is needed, it is copied back to the memory cache. The action of spooling to disk occurs when the Object Caching Service for Java determines that it requires free space in the memory cache. The Object Caching Service for Java automatically moves objects from the memory cache to the disk cache in two cases.
SPOOL
attribute set, the memory object is written to disk before it is removed. Spooling saves the memory object to the disk cache, and avoids re-creating the object when or if it is needed again. You should set the SPOOL
attribute for objects that are expensive to create, especially if the time required to create the object is greater than the cost of loading the object from disk.
"Cache Object Attributes" and "Working with StreamAccess Objects"
See Also:
In some situations, you may want to force one or more objects to be written to the Object Caching Service for Java disk cache. Using the CacheAccess.save()
method, a region, subregion, group, or object is synchronously written to the disk cache (if the object or objects are already in the disk cache, they are not written again).
Calling CacheAccess.save()
on a region, subregion, or group saves all the objects within the region, subregion, or group to the disk cache. During a CacheAccess.save()
method call, if an object is encountered that cannot be written to disk, either because it is not serializable, or for other reasons, the event is recorded in the Object Caching Service for Java log and the save operation continues with the next object.
Objects that you only access directly from disk cache are loaded into the disk cache by calling CacheLoader.createDiskObject()
from the CacheLoader.load()
method. The createDiskObject()
method returns a File
object that the application can use to load the disk object. If the disk object's attributes are not defined for the disk object, set them using the createDiskObject()
method. The system manages local and distributed disk objects differently; the determination of local or distributed is made when the system creates the object, based on the specified attributes.
When CacheAccess.get()
is called on a disk object, the full path name to the file is returned, and the application can open the file, appropriate to its needs.
Disk objects are stored on a local disk and accessed directly from the disk by the application using the Object Caching Service for Java. Disk objects may be shared by all Object Caching Service for Java processes, or they may be local to a particular process, depending on the setting for the DISTRIBUTE
attribute (and the mode the Object Caching Service for Java is running in, either distributed, or local).
Example 4-1 shows a loader object that loads 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 exceptionHandler("exception in file handling", ex) } return file; } }
Example 4-2 shows application code that uses an Object Caching Service for Java disk object. This example assumes the region named "Stock-Market"
is already defined with the "YourObjectLoader"
loader set up in Example 4-1 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 }
StreamAccess objects are objects that are accessed as a stream and are automatically loaded to the disk cache. The object is loaded as an OutputStream
and read as an InputStream
. Smaller StreamAccess objects can be accessed from memory or from the disk cache, while larger StreamAccess objects are streamed directly from disk. The Object Caching Service for Java automatically determines where to access the StreamAccess object based on the size of the object and the capacity of the cache.
The user is always presented with a stream object, an InputStream
for reading and an OutputStream
for writing, regardless of whether the object is in a file or in memory. The StreamAccess object allows the Object Caching Service for Java user to always access the object in a uniform manner, without regard to object size or resource availability.
To create a StreamAccess object, call the
CacheLoader.createStream()
method from the CacheLoader.load()
method when the object is loaded into the cache. The createStream()
method returns an OutputStream
object. The OutputStream
object can be used to load the object into the cache.
If the attributes have not already been defined for the object, they should be set using the createStream()
method. The system manages local and distributed disk objects differently; the determination of local or distributed is made when the system creates the object, based on the attributes.
Example 4-3 shows a loader object that loads a StreamAccess object into the cache.
import oracle.ias.cache.*; class YourObjectLoader extends CacheLoader { public Object load(Object handle, Object args) { OutputStream = out; Attributes attr = new Attributes(); attr.setFlags(Attributes.DISTRIBUTE); try { out = createStream(handle, attr); out.write((byte[])getInfofromsomewhere()); } catch (Exception ex) { // translate exception to CacheException, and log exception throw exceptionHandler("exception in write", ex) } return out; } }
|
Copyright © 2001 Oracle Corporation. All Rights Reserved. |
|