Back to JOC 10.1.2 Tutorial Index

Defining Your Environment

One of the main features of the Java Object Cache is the ability to control how an object in the cache is managed. This is done by defining the environment in which objects are managed. The environment includes defining a private namespace within the cache, a region, defining object associations or object groups and defining the attributes of an object in the cache. The primary methods for setting up a cache environment are CacheAccess.defineRegion(), CacheAccess.defineSubRegion(), CacheAccess.defineGroup(), and CacheAccess.defineObject().

Defining a Region/Sub-Region

All objects within the cache are stored in the context of a region. A region defines a namespace within the cache. All objects within a region must be uniquely named.

To create a region named "Stock-Market", you would invoke the static method defineRegion of CacheAccess. To access objects in the region use the getAccess method to obtain an instance of a CacheAccess object. Attributes may be defined for a region when it is defined. The attributes defined will be inherited by all objects in the region. See the chapter on Cache Object Attributes for details.

import oracle.ias.cache.*;

  Attributes attr = new Attributes();
  attr.setFlags(Attributes.DISTRIBUTE); // make all objects in the region distributed

  try
  {

    // define a region with defined attributes
    CacheAccess.defineRegion("Stock-Market", attr);
    // OR
    // define a region using default attributes
    CacheAccess.defineRegion("Stock-Market");

    CacheAccess cacc = CacheAccess.getAccess("Stock-Market");

    // do something useful with the objects in the region

  }
  catch(CacheException ex)
  {

    // handle exception
  }
Sub-regions are independent namespaces nested within regions. A sub-region is defined within the context of a region via CacheAccess.defineSubRegion() and considers the region to be its parent. The CacheAccess method getSubRegion can be used to access sub-regions. Calling CacheAccess.getParent() within the context of a sub-region returns access to its parent region. Sub-regions can be defined within sub-regions. In the example below (continued from above), sub-region "Oracle" is defined under the region "Stock-Market".
  try
  {

    // define a sub-region with defined attributes
    cacc.defineSubRegion("Oracle", attr);
    // OR
    // define a sub-region while inheriting attributes
    cacc.defineSubRegion("Oracle");

    // obtain access to sub-region
    CacheAccess cacc2 = cacc.getSubRegion("Oracle");

    // do something useful with the objects in the sub-region

  }
  catch(CacheException ex)
  {

    // handle exception

  }
The CacheAccess methods getAccess and getSubRegion are also overloaded to define regions/sub-regions at access time. When calling these methods, an Attributes object can be specified to indicate that if the region/sub-region has not been defined, then it should be created with the attributes supplied. In the case of getAccess, if the region name supplied is of the form "a/b/c/d", then if the boolean parameter subRegion is true, nested region/sub-regions "a", "b", "c", and "d" are created while inheriting the same set of attributes; if subRegion is false, the region "a/b/c/d" is created with the specified attributes. The example below (continued from above) defines and obtains accesses for sub-region "Month1" under "Q1" under "Oracle" under "Stock-Market" and for region "This/Is/My/Region".
  try
  {

    // define and obtain access to Month1
    CacheAccess cacc3 = CacheAccess.getAccess("Stock-Market/Oracle/Q1/Month1", attr, true);
    // OR
    // define and obtain access to Month1 while inheriting attributes
    CacheAccess cacc3 = cacc2.getSubRegion("Q1/Month1", null);
    // OR
    // define and obtain access to Month1 with defined attributes
    CacheAccess cacc3 = cacc2.getSubRegion("Q1/Month1", attr);

    // define and obtain access to region with defined attributes
    CacheAccess cacc4 = CacheAccess.getAccess("This/Is/My/Region", attr, false);

    // do something useful with the objects in the sub-region

  }
  catch(CacheException ex)
  {

    // handle exception

  }
Defining a Group

At times it is useful to create an association between two or more objects within the cache. Objects are typically associated because they will be invalidated together or because they have a common set of attributes. To provide this functionality the cache has the concept of a "group." Any set of cache objects within the same region can be associated using a group (including other groups). Before an object can be associated with a group, the group must be defined. A group is defined with a name and can be defined with its own attributes or inherit the attributes from the group or region it is associated with.

To define a group named "NYSE" in the existing region, "Stock-Market", you would get access to the region then call defineGroup.

import oracle.ias.cache.*;

  try
  {

    CacheAccess cacc = CacheAccess.getAccess("Stock-Market");
    // define the group without attributes
    cacc.defineGroup("NYSE");
    // OR

    // set the default idle time for object
    // in the group to 2 minutes
    Attributes attr = new Attributes();

    attr.setIdleTime(120);

    // define the group with attributes
    cacc.defineGroup("NYSE", attr);
To create a group associated with the previously created group "NYSE", named "Pending IPO", you would call defineGroup as
    cacc.defineGroup("Pending IPO", "NYSE");
    // OR
    cacc.defineGroup("Pending IPO", "NYSE", attr);
To associate an object with a group, you would specify the group name in the put, get or replace call. For example, to put "CPB" into "NYSE", you would do
    cacc.put("CPB", "NYSE", "Day high $27, day low $26.5", attr);
If automatic loading is being used the group should always be included when the get method is called to ensure the object is associated with the group if it needs to be loaded.
    cacc.get("loaded object", "NYSE");
    // OR
    cacc.get("loaded object", "NYSE", args);

    cacc.close();

  }
  catch (CacheException ex)
  {

    // handle exception

  }
Defining an Object

It is often useful to describe to the Caching service how an individual object is to be managed within the cache before the object is loaded into the cache. This can be done by using the CacheAccess.defineObject() method. defineObject is used to associate attributes with an object. Attributes can include such information as how to load the object into the cache, whether the object is local or distributed, how long the object should remain in the cache before being invalidated, etc. If attributes are not defined for an object, the default attributes set for the region or group the object is associated with will be used. Attributes may also be set in the CacheLoader.load() method using the CacheLoader.setAttributes(), CacheLoader.createStream(), CacheLoader.createDiskObject() methods or with CacheAccess.put().

import oracle.ias.cache.*;

  try
  {

    CacheAccess cacc = CacheAccess.getAccess("Stock-Market");

    MyLoader loader = new MyLoader();

    // set the default idle time and the loader for the object
    // and indicate the object is distributed
    Attributes attr = new Attributes();
    attr.setLoader(loader);
    attr.setFlags(Attributes.DISTRIBUTE);

    attr.setIdleTime(120); // in the group to 2 minutes

    // define the objects attributes
    cacc.defineObject("ORCL", attr);

    // object is loaded using the defined loader if not already in the cache
    result = (String)cacc.get("ORCL");
To associate same attributes with the put method
    // since the object is being explicitly "put" into the cache
    // the loader is not defined
    attr.setLoader(null);
    cacc.put("ORCL", "Day high $27, day low $26.5", attr);

    result = (String)cacc.get("ORCL");

    cacc.close();
  }
  catch (CacheException ex)
  {

    // handle exception

  }
Back to JOC 10.1.2 Tutorial Index
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy