How to set up method level caching with annotations using javax.cache / JCache / JSR-107 & Guice

I’ve just spent a day looking at Java caching standards and utilising them in front of a data repository.  It turns out that information, and by this I mean practical information that helps with implementation, is a little thin on the ground.  If you’re not using Spring, you’re stuffed. Hence this post.

JSR-107 is the original java specification for the javax.cache package. It spent the longest time ever to become an approved standard.  Years.  13 years.  That’s right, it was initiated in March 2001 and went final in March 2014.

JCache is the javax.cache API and also the reference implementation of that specification.  Unlike other RIs like Tomcat or Jersey, this one is not meant for production use, which left me scratching my head as to what I should be using.  Confusingly, I’ve found different versions of mplementations in two different name spaces; either javax.cache or org.jsr107.ri.

What am I trying to do?

I want to automagically cache return values from annotated methods. Sounds simple, right? Actually, it is simple once you know how to set it up.  With javax.cache you can do it like this:

Making it work

I eventually found a blog post that pointed me in the right direction about what to include to make it happen.  It took me a while but I’ve found the latest artifacts (they both changed group and artifact IDs since 2011!) and version 1.0 goes like this:

And you’re done.  Really.  The ehcache bindings pick up the annotations, create default caches and populate them exactly as you’d expect.  If you want to configure them up front, you can use an xml file, or modify them on startup.

Configuring the cache

Just using the annotations alone will force the underlying cache provider (in this case ehcache) to create the cache using sensible defaults.  This means a non-expiring, store by value cache.  To change the expiry policy you can do something like this on startup:

Or for more complex use cases you can provide ehcache xml configuration files.  It’s up to you.

So what took me so long.

This took me ages.  Far longer than I thought it would.  The documentation around this sucks.

At the time of writing, there are no publicly available javadocs you can browse on the web.  Well, none that I could find.  No HOW-TO, no simple introduction. If you visit the JSR or JCache pages it’s not much more helpful than maven.  You’re reduced to reading the spec.  To confuse things further, I next realised that the spec expects javax.cache implementations to be Dependency Injection containers rather than caching libraries.  As in Spring, Guice or CDI.  Not ehcache, memcache or whatever.  I had to work all this out before I could even begin to get started.  blah blah blah.  So yeah, that’s what took me so long.

Thanks to the developers though, it really does work well and if you’re in a similar boat I hope this helps.

Written by Tom

1 Comment

Al Rafi Irvan Febrianta

Indeed, i just starting to use caching in spring and couldn’t find better How-To tutorial or Well-Documented API. So yeah,took a long time before i realized how to use it properly.


Leave a Reply