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 4 of 5

Note that the IsSpool, IsLateral, and IsRemote settings are inherited from the default settings. Because the jcs.region.musicCache element is set to DC, it is defined not only to maintain an in-memory cache, but also to use the indexed disk cache as an auxiliary. (The property can be set to a comma-separated list of multiple auxiliaries.) The disk cache is configured to store items in the c:/temp directory. (JCS prefers forward slashes to backslashes.) The remaining attributes configure the disk cache using an IndexedDiskCacheAttribute object; you can read about these attributes in the JCS Javadoc.

Building a sample caching application

Once you understand how to configure JCS, building a caching application is straightforward. The application needs to be able to:

  • Initialize the cache from its configuration file
  • Access a region in the cache
  • Load objects into the cache
  • Retrieve objects from the cache
  • Remove objects from the cache

The cache can be initialized either automatically or manually. If you name your configuration file cache.ccf and put it directly in your CLASSPATH (such as your root build directory), then the first time JCS is invoked it finds the file and initializes appropriately. If you need to store your configuration file elsewhere or name it differently, you can use the org.apache.jcs.utils.props.PropertyLoader's loadProperties() method to load JCS properties from any properties file.

To access a region in the cache, you invoke the JCS class's static getInstance() method, passing it the name of the region to retrieve, as in this example:

JCS cache = JCS.getInstance( "musicCache" );

You can put objects into the cache by invoking the JCS class's put() method, specifying a key and a value:

cache.put(new Integer(1), new Album(1, "Toby Mac", "Diverse City"));

You can then retrieve objects by invoking the JCS class's get() method, passing it the key of the object to retrieve:

Album album = ( Album )cache.get( id );

Finally, you can remove objects from the cache by invoking the JCS class's remove() method, passing it the key of the object to remove:

cache.remove( id );

The only requirement for objects to be stored in the cache is that they must be serializable, meaning that they must implement the java.io.Serializable interface and contain either primitive types, Strings, wrapper classes, or other serializable objects.

Listing 2 shows the code for the Album class, which is the cached object in this example.

Listing 2. The Album class

package com.geekcap.jcs;

public class Album implements java.io.Serializable
{
   private Integer id;
   private String artist;
   private String title;

   public Album() {
   }

   public Album( Integer id, String artist, String title ) {
     this.id = id;
     this.artist = artist;
     this.title = title;
   }

   public Integer getId() {
     return id;
   }

   public void setId( Integer id ) {
     this.id = id;
   }

   public String getArtist() {
     return artist;
   }

   public void setArtist( String artist ) {
     this.artist = artist;
   }

   public String getTitle() {
     return title;
   }

   public void setTitle( String title ) {
     this.title = title;
   }

   public String toString() {
     return artist + ": " + title;
   }
}

The Album class is just a thin, serializable, wrapper around three attributes: id, artist, and title. You can expand your objects to include additional fields and subobjects; you just need to ensure that they are serializable.

  • Print
  • Feedback

Resources

More from JavaWorld