With over forty reusable Java components, the Apache Commons project is a gold mine of time-tested and time-saving tools. If you’re a smart developer, you’ve studied the inventory, scanned the API’s, and set a mental flag for each available component. I actually did that once and felt rather proud of myself at the time, but I guess I forgot most of it. Google is still surprising me with interesting stuff from the Commons. Here’s a good one I recently found, for example: Commons JCS™ – a distributed Java caching system.
Need a quick and easy caching system for your web app? Don’t you dare roll your own. And don’t even think about doing some kind of quick and dirty HashMap. Setting up a simple cache with Commons JCS™ is easy – you can have a basic LRU Cache up and running in minutes. What’s more, the package is mature and sophisticated, so you can get into more advanced features when the need arises. But let’s take a look at a simple setup.
First, the Maven dependency…
Next, create the cache config file as follows. Name it
cache.ccf and put it in your resources directory on the classpath. The first stanza is the default cache. The second is one I created, for example, to cache a list of people (active users) from an expensive API call into the cloud.
You’ll need the following imports…
Get a handle on the cache by name like this…
Finally, here’s an example of a fetch method that attempts to get resources from the cache before going back to the expensive API client request.
That’s all you need to do to setup a cache in your web app fast. But if you’re wondering if Commons JCS™ has the chops to handle something more sophisticated, here’s something to ease your concerns. Beyond simply caching objects in memory, it provides numerous additional features:
- Memory management
- Disk overflow (and defragmentation)
- Thread pool controls
- Element grouping
- Minimal dependencies
- Quick nested categorical removal
- Data expiration (idle time and max life)
- Extensible framework
- Fully configurable runtime parameters
- Region data separation and configuration
- Fine grained element configuration options
- Remote synchronization
- Remote store recovery
- Non-blocking “zombie” (balking facade) pattern
- Lateral distribution of elements via HTTP, TCP, or UDP
- UDP Discovery of other caches
- Element event handling
- Remote server chaining (or clustering) and failover
- Custom event logging hooks
- Custom event queue injection
- Custom object serializer injection
- Key pattern matching retrieval
- Network efficient multi-key retrieval
Before integrating Commons JCS™, be sure to check out possible cache features available on whatever platform you’re building for. For example, you might instead use the built-in DynaCache for IBM WebSphere or the IBM Data Cache for Bluemix. If nothing is already available on your platform, Commons JCS™ can be the way to go.