Newsletter sign-up
View all newsletters

Sign up for our Enterprise Java Newsletter

Enterprise Java

Open source Java projects: Java Caching System

A distributed caching system for enterprise applications

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Enterprise Java specialist Steve Haines joins the Open source Java projects series this month with an introduction to Java Caching System (JCS), a robust enterprise-level caching solution. Steve starts with a quick introduction to caching, discussing the criteria for determining if objects should be cached and whether your application would benefit from a cache. He then shows you how to configure JCS and use it to build a caching application.

The Java Caching System (JCS) is a robust open source caching product released through the Apache Jakarta subproject. It provides the standard features that you would expect of a cache system, such as in-memory caching and algorithms for selectively removing objects from the cache. It also offers more-advanced features, such as indexed disk caching and support for distributed caches.

A JCS cache has a map-like structure in which data is stored in the cache as a name-and-value pair. JCS partitions the cache into regions. Each region has its own configuration as well as its own set of name-value pairs. Each region can:

  • Be sized differently
  • Be implemented differently
  • Contain different data

The keys (the names in the name-and-value pairs) in one region can be the same as keys in other regions. This is important because it enables you to maintain separate caches for different objects all within the same JVM -- and all defined in a single properties file.

Open source licenses

Each of the open source Java projects covered in this series is subject to a license, which you should understand before integrating the project with your own projects. JCS is subject to the Apache License; see Resources to learn more.

This article explores JCS by first showing you how to obtain and install the current release. I'll then explain what a cache is, why you might use one, and whether or not it is the right solution for a specific application. Next, you'll delve into the JCS properties file, which is the best route to understanding JCS. Finally, you'll build a sample caching application that uses JCS.

Get started with JCS

You can download JCS from the downloads page of the JCS project site. As of this writing, the latest version is 1.3. Download the binary distribution (either as a TAR file on Unix systems or a ZIP file on Windows) and decompress it to a local directory on your computer.

The root of the installation directory contains jcs-1.3.jar, which you must add to your CLASSPATH before compiling and running a JCS applications.

Class documentation goldmine

Throughout this article, as well as in your own independent studies, you'll find that the JCS docs directory is an invaluable resource for information about JCS, including the API documentation. The robust Javadoc document is your authority for understanding how to use JCS classes.

You will need two dependencies:

  • Commons Logging
  • Concurrent

From Commons Logging, add commons-logging.jar to your CLASSPATH.

A quick caching primer

A cache is designed to hold objects, typically in memory, for immediate access by an application. An application interacts differently with a cache from the way it interacts with external storage solutions. Typically, an application obtains a connection to a database, executes a query across a network, and parses the results as they are returned. A cache maintains a collection of readily available objects in a robust map-like structure that does not require a network call. Enterprise Java application performance improves exponentially when it accesses reusable objects in a cache after loading them from a database, rather than making remote database calls.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (4)
Login
Forgot your account info?

I like itBy Anonymous on March 7, 2010, 2:02 pmI really appreciate it that you shared with us such a informative post..black onyx earrings

Reply | Read entire comment

Run the GlassFish setupBy Anonymous on February 24, 2009, 6:25 amRun the GlassFish setup script. In the unpacked glassfish directory, there are two .xml files: setup.xml and setup-cluster.xml. Most users will just want to use...

Reply | Read entire comment

Not a survey or comparison ...By Athen on November 13, 2008, 11:05 pmSeems like you were expecting a survey of cache systems. Fair enough, but this article is part of a series focused on open source Java projects, and is only intended...

Reply | Read entire comment

JMemcachedBy Anonymous on November 13, 2008, 5:11 pmI was really surprised to see an article on Java based cache systems without mention of Thimbleware's JMemcached, the java version of the popular caching system...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources

More from JavaWorld