JCache, JCS and its extra module

Extra module of commons-jcs provides some interesting basic features to use with JCache.

This post is just a quick overview of them.


First a little CDI extension is provided to allow to get injected without any further action a default CacheManager and CacheProvider. Just do:

private CacheManager manager;

And you’ll get it injected.

Side note: if you already provide one CacheManager yours will be used.


Then extra module provides you basic classes to write custom loaders/writers.

  • CacheLoaderAdapter/CacheWriterAdapter: adapters allowing you to write single operations (write/delete/load…) and wiring them to plural ones (loadAll, writeAll, …).
  • CompositeCacheLoader/CompositeCacheWriter: a facade for multiple readers/writers. For readers the first answering is used, for writers all are always notified.
  • AsyncCacheWriter: a writer using a pool to execute its tasks

Side note: all these classes used a diabolic hook implementing JCache Factory too this way you can use them directly in your JCache configuration:

new MutableConfiguration<String, String>()
  // no need of new SingletonFactory<>(new CacheLoaderAdapter....));
  .setCacheLoaderFactory(new CacheLoaderAdapter<String, String>() {
        public String load(final String key) throws CacheLoaderException {
            return ...;


Finally JCacheFilter is a simple Servlet Filter allowing you to use JCache to cache responses.

It targets normal and gzip pages and handles status, content-length, context-type, cookies, headers and output.

The filter works once defined but it is configurable (more or less JCache CompleteConfiguration) using its init parameters.

For CacheManager:

  • configuration: uri
  • all init parameters are passed as properties of the cache manager

For Web Cache:

  • cache-name: web cache name (default org.apache.commons.jcs.jcache.extras.web.JCacheFilter)
  • read-through: is the cache read through. If yes cache-loader-factory should be configured
  • write-through: is the cache write through. If yes cache-writer-factory should be configured
  • cache-loader-factory: the CacheLoader factory to use to get missing values
  • cache-writer-factory: the CacheWriter factory to use to propagate write/delete operations
  • expiry-policy-factory: ExpiryPolicy factory, default is Eternal (JCache default)
  • management-enabled: is JMX enabled
  • statistics-enabled: are statistics enabled (put/get/… counts + durations)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s