Introduction to JCache JSR 107

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "Resin has supported caching, session replication (another form of caching), and http proxy caching for over ten years. When you use Resin caching you are using the same platfo...")
 
Line 1: Line 1:
Resin has supported caching, session replication (another form of caching), and http proxy caching for over ten years.
+
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.
+
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. The Cache object in JCache looks like a java.util.ConncurrentHashMap.
+
[http://jcp.org/en/jsr/detail?id=107 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.
 
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.
  
Line 34: Line 34:
 
}
 
}
  
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
protected void doGet(HttpServletRequest request, HttpServletResponse response)  
 +
                                                                                      throws ServletException, IOException {
 
response.setContentType("text/html");
 
response.setContentType("text/html");
 
response.getWriter().append("<html><head></head><body><p>");
 
response.getWriter().append("<html><head></head><body><p>");

Revision as of 00:00, 23 January 2013

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.


package hello.world;

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

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
	
	Cache<String,String> cache;
	
	public Cache<String, String> cache() {
		if (cache == null) { //building a cache
			CacheManager manager = Caching.getCacheManager("cacheManagerHello");
			CacheBuilder<String,String> builder = manager.createCacheBuilder("a");
			cache = builder.build();
		}
		return cache;
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                                                                       throws ServletException, IOException {
		response.setContentType("text/html");
		response.getWriter().append("<html><head></head><body><p>");

		String helloMessage = cache().get("hello message");
		
		if (helloMessage == null) {
			helloMessage = new StringBuilder(20)
				.append("Hello World ! <br />")
				.append(System.currentTimeMillis()).toString();

			cache().put("hello message", helloMessage); // <-------------- putting results in the cache
		}

		response.getWriter().append(helloMessage);
		

		response.getWriter().append("</p></body></html>");
	}

}

Personal tools
TOOLBOX
LANGUAGES