Back to JOC 10.1.2 Tutorial Index

Setting Cache Object Attributes

One of the main features of the Java Object Cache is the ability to define how each object in the cache is managed. These object characteristics are known as "attributes." The management of these attributes is encapsulated in the Attributes class. Attributes fall into 2 categories: Those that must be defined when or before an object is loaded into the cache and those that can be modified on cached objects. The first set of attributes includes the loader object and "flag" attributes which are on/off values that determine the basic characteristics of the object's management. This would include whether the object is distributed, synchronized, if the object should be spooled, etc. (for a complete list see the Attributes class). These attributes can only be set by the CacheAccess.define*() methods (see Defining Your Environment), CacheAccess.put() or from the CacheLoader.load() method using CacheLoader.setAttributes(), CacheLoader.createStream(), or CacheLoader.createDiskObject() methods (see Implementing a CacheLoader).

Attributes in the second category, those that can be modified on a cached object, include time to live, default time to live, idle time and event handlers. These attributes can be set via the CacheAccess.define*() methods, the CacheLoader.setAttributes(), CacheLoader.createStream(), or CacheLoader.createDiskObject() methods and can be reset via the CacheAccess.resetAttributes() method.

To illustrate the use of attributes suppose you want all of the objects in the "Stock-Market" region to, by default, invalidate automatically 10 seconds after they are loaded into the cache (see Attributes.setDefaultTimeToLive()), to be loaded with a StockLoader object, to use the StockIEvent event handler, to be distributed and can be spooled.

import oracle.ias.cache.*;

  try
  {

    CacheAccess cacc = CacheAccess.getAccess("Stock-Market");
    Attributes attr = new Attributes();
    StockLoader loader = new StockLoader();
    SunLoader sunLoader = new SunLoader();
    StockIEvent handler = new StockIEvent();
    StockObj orcl, sunw;

    // set distribute and spool flags
    attr.setFlags(Attributes.DISTRIBUTE | Attributes.SPOOL);

    // set the loader
    attr.setLoader(loader);

    // set the invalidate event handler
    attr.setEventListener(handler);

    // default time to live is 10 seconds
    attr.setDefaultTimeToLive(10);

    // define the attributes as the defaults for the region
    CacheAccess.defineRegion("Stock-Market", attr);
    cacc = CacheAccess.getAccess("Stock-Market");

    // override default setting for the flags and the loader for the "SUNW" object
    // the "SUNW" object is local and will not be spooled
    // time to live and event handler are the same
    attr = new Attributes();
    attr.setLoader(sunLoader);
    defineObject("SUNW", attr);

    // get distributed object using StockLoader
    orcl = (StockObj)cacc.get("ORCL");

    // get local object using SunLoader
    sunw = (StockObj)cacc.get("SUNW");

    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