main website home
  • About this blog

    This blog features updates, opinions, and technical notes from Caucho engineers about Caucho products, the enterprise Java industry, and PHP. Caucho Technology is the creator of the Resin Application Server and the Quercus PHP in Java engine. A leader in Java performance since 1998, Caucho is a Sun JavaEE licensee with over 9000 customers worldwide.
  • Tags

    ajaxworld bam candi cdi cloud cluster comet configuration deploy devoxx eclipse ejb embedded flash flex google app engine hessian hmtp ioc java ee 6 javaone javazone jms messaging newsletter nyjug osgi php pomegranate quercus resin resin 4.0 REST servlet sfjug silicon valley code camp spring testing training tssjs watchdog webbeans web profile websockets wordpress
  • Meta

    • Register
    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
« HMTP - publish/subscribe packets
Join us on Facebook! »

Resin 4.0 jcache (distributed caching)

Resin cloud

With Resin 4.0, we can expose our distributed caching/store capabilities to all developers, using the javax.cache interface. Internally, our distributed cache builds on 10 years of work with distributed sessions. So the jcache support isn’t a new capability, really, it’s just newly available for developers.

As an introduction, I want to show the prototypical code sample (it’s an injected Map), and the configuration (one XML tag in the resin-web.xml). The configuration is simple because Resin’s clustered cache is designed for Resin and automatically inherits the cluster and triad configuration from the Resin deployment.

Distributed caching reduces load on the database, improves latency, and can improve reliability by providing a failover capability in a load-balancing configuration. Essentially, you store serialized objects in a Map and the cache makes the Map entries visible across the entire cluster pod automatically.

code samples

In the example below, I’m just storing a fixed string in the cache and retrieving it. Because the cache is distributed, a second request on a different server would return the old data, and skip the processing step.

package example;

import java.io.*;
import javax.servlet.*;
import javax.webbeans.Current;
import javax.cache.Cache;

public class MyServlet extends GenericServlet {
  @Current Cache _cache;

  public void service(ServletRequest req, ServletResponse res)
  {
     PrintWriter out = res.getWriter();

     String data = (String) _cache.get("my-data");
     if (data != null) {
       out.println("cached data: " + data);
     }
     else {
       data = generateComplicatedData();
       _cache.put("my-data", data);
       out.println("new data: " + data);
     }
  }
}

I’m using WebBeans injection to decouple my application code from the configuration. The @javax.webbeans.Current injects the configured Cache as defined in the WEB-INF/resin-web.xml.

configuration

To configure the Cache, I just need to instantiate a ClusterCache instance (in com.caucho.cluster), and give it a name. The name is required and must be unique in the web-app, because the clustered cache needs to manage multiple caches and keep them distinct, but Resin also needs to manage them in a unified way. The unique name avoids any entry conflicts.

<web-app xmlns="http://caucho.com/ns/resin"
               xmlns:cluster="urn:java:com.caucho.cluster">

  <cluster:ClusterCache name="comment-cache"/>

</web-app>

For specialized uses, the ClusterCache has configuration fields, so you can configure things like the local caching timeouts and expire times, and specify the replication requirements. By default, data is replicated on the triad — a reason why the triad is critical to Resin’s caching.

The ClusterCache can avoid complicated configuration because all the important clustering configuration is already specified in the resin.xml. Because the caching is integrated with Resin’s clustering, the cache handles dynamically added and removed servers. From an administrator’s perspective, it just works automatically.

Tags: cache, cluster, resin 4.0, triad

This entry was posted on Wednesday, January 21st, 2009 at 11:43 am and is filed under Engineering. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.


Caucho Technology is proudly powered by WordPress and Quercus®
Entries (RSS) and Comments (RSS).

  • HOME |
  • CONTACT US |
  • DOCUMENTATION |
  • BLOG |
  • WIKI 4 |
  • WIKI 3 |
  • Resin: Java Application Server
Copyright (c) 1998-2012 Caucho Technology, Inc. All rights reserved.
caucho® , resin® and quercus® are registered trademarks of Caucho Technology, Inc.
resin® is a cloud optimized, java® application server that supports the java ee webprofile ®