Java Cache Tutorial with Method Annotations (CDI)
From Resin 4.0 Wiki
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
- Add a @CacheResult annotation to the method you want to cache
- 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/>