VOOZH about

URL: https://www.javacodegeeks.com/2017/12/caching-method-results-jcache.html

⇱ Caching method results with JCache - Java Code Geeks


In JCache there is a handy functionality that transparently caches the result of methods. You can annotate methods of managed beans with @CacheResult and the result of the first call will be returned again without calling the actual method a second time.

import javax.cache.annotation.CacheResult;
// ...

public class Calculator {

 @CacheResult
 public String calculate() {
 // do some heavy lifting...
 LockSupport.parkNanos(2_000_000_000L);

 return "Hi Duke, it's " + Instant.now();
 }
}

If the bean is injected and the method calculate called, the result will be cached after the first call. Per default this mechanism doesn’t cache and return exceptions.

We can include the calculator in a JAX-RS resource as follows:

@Path("calculation")
public class CalculationResource {

 @Inject
 Calculator calculator;

 @GET
 public String calculation() {
 return calculator.calculate();
 }
}

Calling that HTTP resource will return the same value for all subsequent invocations.

For this example to run in Java EE application servers we for now have to declare the interceptor that is responsible for caching the result. This is due to JCache not being included in the EE umbrella. Therefore this small configuration overhead needs to be done for now.

If you want to run this example in WildFly specify the interceptor in the beans.xml:

<interceptors>
 <class>org.infinispan.jcache.annotation.CacheResultInterceptor</class>
</interceptors>

WildFly uses Infinispan that needs to be added in the pom.xml in the correct version as well.

<dependency>
 <groupId>javax.cache</groupId>
 <artifactId>cache-api</artifactId>
 <version>1.0.0</version>
</dependency>
<dependency>
 <groupId>org.infinispan</groupId>
 <artifactId>infinispan-jcache</artifactId>
 <version>8.2.4.Final</version>
</dependency>
Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Caching method results with JCache

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

πŸ‘ Photo of Sebastian Daschner
Sebastian Daschner
December 28th, 2017Last Updated: December 27th, 2017
0 146 1 minute read

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz