|
Back to JOC 10.1.2 Tutorial Index
Remote Cache Access
The Cache Service provides four methods to access the contents of remote caches. The protected methods CacheLoader.getFromRemote() and CacheLoader.netSearch() can only be accessed from CacheLoader.load(). getFromRemote searches a specified remote cache for a specified object to load into the local cache, while netSearch searches all caches in the system for a specified object to load into the local cache. CacheAccess.replaceRemote() replaces a specified object in a specified remote cache. CacheAccess.getAllCached() does not change the contents of caches, but returns a vector of all instances of a specified object in all caches in the distributed system.
If the object exists in the remote cache, getFromRemote will load it into the local cache and return a reference to it. If the object does not exist in the remote cache, the remote cache will attempt to load it first. If the useRemoteTtl boolean parameter is set, the local object attributes such as time to live or idle time will be set to the remote object values. netSearch is used in the same way, except there is no need to specify any particular remote cache because it searches all remote caches, and remote caches will not attempt to load the object if it does not already exist.
The examples below demonstrate using getFromRemote/netSearch in implementing CacheLoader.load().
import oracle.ias.cache.*;
class MyLoader extends CacheLoader
{
public Object load(Object handle, Object args) throws CacheException
{
Object obj = null;
try
{
obj = getFromRemote(handle, (CacheAddress)args, 10000, true); // use remote ttl
}
catch (CacheException ex)
{
throw ex;
}
return obj;
}
}
****************************************
import oracle.ias.cache.*;
class MyLoader extends CacheLoader
{
public Object load(Object handle, Object args) throws CacheException
{
Object obj = null;
try
{
obj = netSearch(handle, 10000, true); // use remote ttl
}
catch (CacheException ex)
{
throw ex;
}
return obj;
}
}
While getFromRemote can be used with memory, disk, and stream objects, netSearch can only be used with memory objects.
When calling replaceRemote, the contents in the remote target cache take priority. If the object being replaced is marked as a local object in the remote cache, or if the remote cache is full, then the operation will fail with an exception. If the target address is the local cache address, then the operation will be treated as a local replace. If the specified object does not exist in the remote cache, then the operation will be treated as a remote put. replaceRemote is also limited to memory objects.
The example below demonstrates using replaceRemote. After execution, a cacc.get() in Cache1.java on object "obj" will return the string "object 1 replacement".
Cache1.java
-----------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
cacc.put("obj", "object 1");
Cache2.java
-----------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
CacheAddress cache1 = getCache1CacheAddress();
cacc.replaceRemote("obj", "object 1 replacement", cache1);
Unlike getFromRemote/netSearch and replaceRemote, getAllCached does not change the contents of any cache. This method can deal with memory, stream, or disk objects, with the limitation that it can only deal with one type each call. In other words, when calling getAllCached on an object, that object's basic type (memory/stream/disk) should be consistent across all caches in the distributed system. If no cache currently has an object matching that name in cache, an empty vector will be returned. Depending on whether you care about any possible exceptions thrown at remote caches while trying to locate the object, you can set the optional boolean parameter ignoreRemoteEx. By default, it is set to true, and all remote exceptions are ignored.
The example below demonstrates using getAllCached.
Cache1.java
-----------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
MemObjType1 obj = new MemObjType1();
cacc.put("obj", obj);
Cache2.java
-----------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
MemObjType2 obj = new MemObjType2();
cacc.put("obj", obj);
...
Cache9.java
-----------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
MemObjType9 obj = new MemObjType9();
cacc.put("obj", obj);
Cache10.java
------------
import oracle.ias.cache.*;
Attributes attr = new Attributes();
attr.setFlags(Attributes.DISTRIBUTE);
CacheAccess.defineRegion("region", attr);
CacheAccess cacc = CacheAccess.getAccess("region");
Vector objects = cacc.getAllCached("obj");
Note that all objects that need to be communicated between caches must be Serializable objects.
Back to JOC 10.1.2 Tutorial Index
|