Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Open source Java projects: Java Caching System

A distributed caching system for enterprise applications

  • Print
  • Feedback

Page 3 of 5

  • The default region defines the default configuration for all regions unless it is overridden explicitly by one of the other regions.
  • Next is a list of predefined (that is, user-defined) cache regions, which in this case includes the musicCache that I'll use in the upcoming example.
  • Auxiliary caches define auxiliaries that can be plugged into a cache region. Although each cache region must have one (and only one) memory auxiliary, it can have any number of other auxiliaries that can hold cached data. In this example I create an indexed disk cache, but you can also define lateral and remote auxiliaries. A lateral auxiliary can replicate your cached data to other caches via a TCP socket or JGroups protocol stack. A remote auxiliary can replicate data to other caches via Remote Method Invocation (RMI).

Each region can define cache attributes as well as element attributes. A cache attribute defines a configuration option for the cache, whereas an element attribute defines a configuration option for the elements in the cache. Here's a summary of the cache attribute options:

  • MaxObjects: This is the maximum number of objects allowed in memory.
  • MemoryCacheName: This property allows you to define the memory manager to use as your MemoryCache. The default memory manager implements a LRU strategy.
  • UseMemoryShrinker: This option allows JCS to iterate periodically over the cache, looking for objects that can be removed (items that have expired or have exceeded their maximum memory-idle time). The default value is false.
  • MaxMemoryIdleTimeSeconds: If the memory shrinker is enabled, this property tells JCS how long an object can remain idle before the shrinker removes it (and spools it to disk if an indexed disk cache has been created). The default value is -1, which disables this option.
  • ShrinkerIntervalSeconds: If the memory shrinker is enabled, this property tells JCS how often to run the shrinker. The default value is 60 seconds.
  • DiskUsagePattern: If a disk cache is enabled, this property tells JCS how to persist data when the memory cache is full. The default value is SWAP, which spools items to disk only when the memory cache is full. The other option is UPDATE, which persists all data out to disk, but only when data is updated. If a JDBC auxiliary has been defined as a disk cache, all objects remain in memory (until the memory is full) and are also persisted to a database, which provides for good performance as well as reliability.

And here are the element attribute options:

  • IsEternal: If an element is eternal then it cannot be removed from the cache because it exceeds its maximum life. This option defaults to true.
  • MaxLifeSeconds: If elements are not eternal, this option defines the maximum life of each object before it is removed. If the memory shrinker is running, objects are removed by the shrinker; if not, they are removed when they're accessed. This option defaults to -1, which disables the option.
  • IsSpool: This option defines whether or not an element can be spooled out to disk. It defaults to true.
  • IsLateral: This option defines whether or not an element can be sent to a lateral cache. It defaults to true.
  • IsRemote: This option defines whether or not an element can be sent to a remote cache. Defaults to true.

In Listing 1, I created a region named musicCache that holds up to 1,000 items in memory. Its memory manager uses a LRU algorithm: when the cache is full and JCS needs to make room for new items, it will remove items that have not been recently accessed. It has the memory shrinker enabled, and the shrinker will run every 60 seconds. It will evict items that sit idle for more than 60 minutes (3,600 seconds.) Its items are not eternal, and they can be written out to disk, to a lateral cache, or to a remote cache.

  • Print
  • Feedback

Resources

More from JavaWorld