Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
Page 5 of 5
Listing 3 shows the contents of the MusicStore class, which demonstrates how to cache Albums.
package com.geekcap.jcs;
import org.apache.jcs.JCS;
import org.apache.jcs.access.exception.CacheException;
public class MusicStore
{
private JCS cache;
public MusicStore()
{
try
{
// Load the cache
cache = JCS.getInstance( "musicCache" );
// Initialize the cache
cache.put( new Integer( 1 ),
new Album( 1, "Toby Mac", "Diverse City" ) );
cache.put( new Integer( 2 ),
new Album( 2, "Pillar", "Fireproof" ) );
cache.put( new Integer( 3 ),
new Album( 3, "Audio Adrenaline", "Underdog" ) );
}
catch( CacheException e )
{
e.printStackTrace();
}
}
public void addAlbum( Album album )
{
try
{
cache.put( album.getId(), album );
}
catch( CacheException e )
{
e.printStackTrace();
}
}
public Album getAlbum( Integer id )
{
return ( Album )cache.get( id );
}
public void removeAlbum( Integer id )
{
try
{
cache.remove( id );
}
catch( CacheException e )
{
e.printStackTrace();
}
}
public static void main( String[] args )
{
MusicStore musicStore = new MusicStore();
musicStore.addAlbum( new Album( 4, "The O.C. Supertones",
"Supertones Strike Back" ) );
Album album = musicStore.getAlbum( 1 );
System.out.println( "Album 1: " + album );
album = musicStore.getAlbum( 4 );
System.out.println( "Album 4: " + album );
musicStore.removeAlbum( 4 );
album = musicStore.getAlbum( 4 );
System.out.println( "Album 4: " + album );
}
}
The MusicStore class retrieves the musicCache region from JCS in its constructor. Because the cache configuration file is named cache.ccf and is in the CLASSPATH, the getInstance() method causes JCS to load the cache properties and create the region. Subsequent getInstance() invocations would simply return the region that has already been created. The constructor then loads three albums into the
cache by invoking the put() method.
The main() method demonstrates how to add an album, retrieve an album, and remove an album from the MusicStore, which simply delegates to one of the JCS cache methods.
Although this example runs as a standard Java class, a more realistic example would integrate these classes into an enterprise Java application. For example, the music store could be expanded to check for an object in the cache. If it doesn't find the object there, it would then query a database for the requested object, putting the result back into the cache. Then a servlet, a business object, or a Spring bean could use the music store as a service object.
The primary benefit of a cache is that objects can be served from local memory faster than they can be loaded from an external data source, such as a database located across a network. The drawback is that caches can consume a considerable amount of memory, and if objects and their usage patterns are not analyzed thoroughly, cache management can introduce another bottleneck into your application. If a cache appears to be a good fit once you analyze your objects, JCS is a great option.
Read more about Enterprise Java in JavaWorld's Enterprise Java section.
More from JavaWorld