Java Cache Tutorial with Method Annotations (CDI)

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
Ferg (Talk | contribs)
(Created page with "{{Caching}} {{Cookbook}} Faster application performance is possible with Java caching by saving the results of long calculations and reducing database load. The Java caching ...")
Newer edit →

Revision as of 00:00, 28 January 2012

Squirrel-48.pngCookbook-48.png

Faster application performance is possible with Java caching by saving the results of long calculations and reducing database load. The Java caching API is being standardized with jcache. In combination with Java Dependency Injection (CDI), you can use caching in a completely standard fashion in the Resin Application Server. You'll typically want to look at caching when your application starts slowing down, or your database or other expensive resource starts getting overloaded. Caching is useful when you want to:

  • Improve latency
  • Reduce database load
  • Reduce CPU use
  1. Add a @CacheResult annotation to the method you want to cache
  2. Use Java Dependency Injection (CDI) to get the bean

MyBean.java

 package org.example.mypkg;

 public class MyBean {
   @CacheResult
   String doLongOperation(String key)
   {
     ...
   }
 }

MyServlet.java

 package org.example.mypkg;

 public class MyServlet extends GenericServlet {
   @Inject MyBean _bean;

   public void service(ServletRequest req, ServletResponse res)
     throws IOException, ServletException
   {
     PrintWriter out = res.getWriter();
 
     String result = _bean.doLongOperation("test");
  
     out.println("test: " + result);
   }
 }

WEB-INF/beans.xml

 <beans/>
Personal tools
TOOLBOX
LANGUAGES