Performance Zone is brought to you in partnership with:

Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container. Bill has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

Introduction to JCache JSR 107

  • submit to reddit

Resin has supported caching, session replication (another form of caching), and http proxy caching in cluster environments for over ten years. When you use Resin caching, you are using the same platform that has the speed and scalability of custom services written in C like NginX with the usability of Java, and the industry platform Java EE. JCache JSR 107 is a distributed cache that has a similar interface to the HashMap that you know and love. To be more specific, the Cache object in JCache looks like a java.util.ConncurrentHashMap. In addition, JCache JSR 107 defines integration with CDI (as well as Spring and Guice). You can decorate services with interceptors that apply caching to the services just by defining annotations.

Resin 4 has support for JCache, and JCache support is required for Java EE 7.

Let's look at a small example to see how easy is to get started with JCache.


import javax.cache.Cache;
import javax.cache.CacheBuilder;
import javax.cache.CacheManager;
import javax.cache.Caching;

public class HelloServlet extends HttpServlet {
	Cache cache;
	public Cache cache() {
		if (cache == null) { //building a cache
			CacheManager manager = Caching.getCacheManager("cacheManagerHello");
			CacheBuilder builder = manager.createCacheBuilder("a");
			cache =;
		return cache;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                                                                       throws ServletException, IOException {

"); String helloMessage = cache().get("hello message"); if (helloMessage == null) { helloMessage = new StringBuilder(20) .append("Hello World !
") .append(System.currentTimeMillis()).toString(); cache().put("hello message", helloMessage); // <-------------- putting results in the cache } response.getWriter().append(helloMessage); response.getWriter().append("

"); } }

The above works out fairly well, but what if we want to periodically change the helloMessage. Let's say we get 2,000 requests a second, but every 10 seconds or so we would like to regenerate the helloMessage.

The message might be:

Hello World ! 

Later we would want it to change.

If we wanted it to change every 10 seconds after it was last accessed, we would do this:

cache = builder.setExpiry(ExpiryType.ACCESSED, new Duration(TimeUnit.SECONDS, 10)).build();

For this example, we want to change it every 10 seconds after is was last modified. We would set up the timeout on the creation as follows:

cache = builder.setExpiry(ExpiryType.MODIFIED, new Duration(TimeUnit.SECONDS, 10)).build();

This would go right in the cache method we defined earlier.

public Cache<String, String> cache() {
		if (cache == null) {
			CacheManager manager = Caching.getCacheManager("cacheManagerHello");
			CacheBuilder<String,String> builder = manager.createCacheBuilder("b");
			cache = builder.setExpiry(ExpiryType.MODIFIED, new Duration(TimeUnit.SECONDS, 10)).build();
		return cache;

Resin's JCache implementation is built on top Resin distributed cache architecture. You get replication, and data redundancy built in.

Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container.

Caucho's Resin OpenSource Servlet Container

Java EE Web Profile Servlet Container

Caucho's Resin 4.0 JCache blog post

Published at DZone with permission of its author, Bill Digman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Andy Jefferson replied on Wed, 2013/02/13 - 3:58am

This is out-of-date now. Version 0.6 of javax.cache was released a few days ago (and version 0.61 since then!) and there is no CacheBuilder (changes were made to how a cache is created and how it is configured). See

Scott Bartram replied on Wed, 2013/02/13 - 7:59pm

At this point, JCache will not be required for JEE 7.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.