Posted By:
billdigman
Posted On:
Friday, February 8, 2013 01:55 PM
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
More>>
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.
01.
package
hello.world;
02.
03.
04.
05.
06.
import
javax.cache.Cache;
07.
import
javax.cache.CacheBuilder;
08.
import
javax.cache.CacheManager;
09.
import
javax.cache.Caching;
10.
...
11.
12.
13.
14.
15.
@WebServlet
(
"/HelloServlet"
)
16.
public
class
HelloServlet
extends
HttpServlet {
17.
18.
Cache cache;
19.
20.
public
Cache cache() {
21.
if
(cache ==
null
) {
22.
CacheManager manager = Caching.getCacheManager(
"cacheManagerHello"
);
23.
CacheBuilder builder = manager.createCacheBuilder(
"a"
);
24.
cache = builder.build();
25.
}
26.
return
cache;
27.
}
28.
29.
30.
31.
32.
protected
void
doGet(HttpServletRequest request, HttpServletResponse response)
33.
throws
ServletException, IOException {
34.
response.setContentType(
"text/html"
);
35.
response.getWriter().append("
36.
37.
38.
39.
<p>");
40.
41.
42.
43.
44.
String helloMessage = cache().get(
"hello message"
);
45.
46.
if
(helloMessage ==
null
) {
47.
helloMessage =
new
StringBuilder(
20
)
48.
.append("Hello World !
49.
50.
51.
52.
53.
")
54.
.append(System.currentTimeMillis()).toString();
55.
56.
57.
58.
59.
cache().put(
"hello message"
, helloMessage);
60.
}
61.
62.
63.
64.
65.
response.getWriter().append(helloMessage);
66.
67.
68.
69.
70.
71.
response.getWriter().append("</p>
72.
73.
74.
75.
");
76.
}
77.
78.
79.
80.
81.
}
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:
1.
Hello World !
2.
1358979745996
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:
1.
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:
1.
cache = builder.setExpiry(ExpiryType.MODIFIED,
new
Duration(TimeUnit.SECONDS,
10
)).build();
This would go right in the cache method we defined earlier.
01.
public
Cache<String, String> cache() {
02.
if
(cache ==
null
) {
03.
CacheManager manager = Caching.getCacheManager(
"cacheManagerHello"
);
04.
05.
CacheBuilder<String,String> builder = manager.createCacheBuilder(
"b"
);
06.
cache = builder.setExpiry(ExpiryType.MODIFIED,
new
Duration(TimeUnit.SECONDS,
10
)).build();
07.
}
08.
return
cache;
09.
}
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
<<Less