Java Cache Tutorial with Method Annotations (CDI)

From Resin 4.0 Wiki

(Difference between revisions)
Jump to: navigation, search
(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 ...")
 
Line 1: Line 1:
 
{{Caching}} {{Cookbook}}
 
{{Caching}} {{Cookbook}}
  
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
+
Java caching can speed application performance and lower database load by annotating cacheable methods. The Java caching API (JCache) includes standard
to look at caching when your application starts slowing down, or your database or other expensive resource starts getting overloaded. Caching is
+
method annotations that let you add caching just by annotating your methods. Assuming your bean is a Java Dependency Injection (CDI) bean, your method will
useful when you want to:
+
be cached automatically. When using an application server like the Resin application server that supports both CDI and JCache, you can add caching easily
 +
without much configuration.
  
 +
You'll want to cache to 
 
* Improve latency
 
* Improve latency
 
* Reduce database load
 
* Reduce database load
 
* Reduce CPU use
 
* Reduce CPU use
 +
 +
With the caching annotations, you can add caching with the following two steps:
  
 
# Add a @CacheResult annotation to the method you want to cache
 
# Add a @CacheResult annotation to the method you want to cache
 
# Use Java Dependency Injection (CDI) to get the bean
 
# Use Java Dependency Injection (CDI) to get the bean
 +
 +
In the example, we'll inject the MyBean into a servlet for testing using the CDI @Inject method.
  
 
=== MyBean.java ===
 
=== MyBean.java ===

Revision as of 00:00, 28 January 2012

Squirrel-48.pngCookbook-48.png

Java caching can speed application performance and lower database load by annotating cacheable methods. The Java caching API (JCache) includes standard method annotations that let you add caching just by annotating your methods. Assuming your bean is a Java Dependency Injection (CDI) bean, your method will be cached automatically. When using an application server like the Resin application server that supports both CDI and JCache, you can add caching easily without much configuration.

You'll want to cache to

  • Improve latency
  • Reduce database load
  • Reduce CPU use

With the caching annotations, you can add caching with the following two steps:

  1. Add a @CacheResult annotation to the method you want to cache
  2. Use Java Dependency Injection (CDI) to get the bean

In the example, we'll inject the MyBean into a servlet for testing using the CDI @Inject method.

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