How to reduce database load by caching

From Resin 3.0

(Difference between revisions)
Jump to: navigation, search
(WEB-INF/resin-web.xml)
 
(3 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
{{Cleanup}}
 
[[Category: HowTo]]
 
[[Category: HowTo]]
  
Line 13: Line 14:
 
   <%
 
   <%
 
         String comment = (String) _cache.get("comment:259");
 
         String comment = (String) _cache.get("comment:259");
 
+
 
         if (value == null) {
 
         if (value == null) {
 
             comment = lookup_comment(259);
 
             comment = lookup_comment(259);
Line 26: Line 27:
 
I've used [[WebBeans]]-style [[dependency injection]] here because I want to keep my code free of dependencies.  Any configuration stuff belongs in the config file.
 
I've used [[WebBeans]]-style [[dependency injection]] here because I want to keep my code free of dependencies.  Any configuration stuff belongs in the config file.
  
I'll configure the cache in my resin-web.xml.  The cache type is a TriadCache, which stores the data in triplicate (assuming I have at least 3 servers.)   
+
I'll configure the cache in my resin-web.xml.  The cache type is a TriadCache, which stores the data in triplicate (assuming I have at least 3 servers.)  The [[Triad]] is Resin's strategy for managing clusters.
  
 
The cache needs a unique name to distinguish it from other caches.
 
The cache needs a unique name to distinguish it from other caches.

Latest revision as of 16:23, 7 December 2011

40px-Edit-clear.png This article requires cleanup and may refer to a legacy version of Resin.

Please visit http://www.caucho.com/documentation/ for the most up-to-date documentation.

If I want to reduce database load and improve performance, I can cache database values in Resin's distributed cache. As long as the content exists in the cache, my application can avoid going to the database entirely. Because Resin's distributed cache is integrated with the clustering, it will ensure updates will be available for all servers consistently.

For example, I might have a blog commenting system, and want to avoid checking the database every time for the same comment text. If I can store it with Resin, I can avoid the database entirely. My code checks the cache first for the comment (#259). If it's missing, I'll ask the database for the content.

Because my comment storing code also saves updates to the cache, I'll automatically see edits, even if the changes happen on a different server.

cache.jsp

 <%@ page import="com.caucho.cluster.*" %>
 <%! @javax.webbeans.Current Cache _cache; %>
 <%
        String comment = (String) _cache.get("comment:259");

       if (value == null) {
            comment = lookup_comment(259);
            _cache.put("comment:259", value);
       }

       out.println("comment: " + comment);
 %>

The com.caucho.cluster.Cache is just a HashMap, so I can get and put just using Map methods.

I've used WebBeans-style dependency injection here because I want to keep my code free of dependencies. Any configuration stuff belongs in the config file.

I'll configure the cache in my resin-web.xml. The cache type is a TriadCache, which stores the data in triplicate (assuming I have at least 3 servers.) The Triad is Resin's strategy for managing clusters.

The cache needs a unique name to distinguish it from other caches.

WEB-INF/resin-web.xml

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

     <cluster:TriadCache name="my-cache"/>

 </web-app>
Personal tools