DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • The Power of Caching: Boosting API Performance and Scalability
  • DevSecOps: Enhancing Security With Vulnerability Scanning of Images and Source Code in CI/CD
  • Designing High-Performance APIs
  • API and Database Performance Optimization Strategies

Trending

  • Microservices With Apache Camel and Quarkus
  • Supercharge Your Communication With Twilio and Ballerina
  • Maximising Data Analytics: Microsoft Fabric vs. Power BI
  • The State of Data Streaming for Digital Natives (Born in the Cloud)
  1. DZone
  2. Data Engineering
  3. Data
  4. Caching Method Results With JCache

Caching Method Results With JCache

Take a look at how to make use of JCache, a simple annotation, and a few associated dependencies to store method results.

Sebastian Daschner user avatar by
Sebastian Daschner
·
Jan. 02, 18 · Tutorial
Like (3)
Save
Tweet
Share
11.84K Views

Join the DZone community and get the full member experience.

Join For Free

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 is called, the result will be cached after the first call. By 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 on 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, which needs to be added in the pom.xml.

<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>


This post was reposted from my newsletter issue 011.

Cache (computing)

Published at DZone with permission of Sebastian Daschner. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Power of Caching: Boosting API Performance and Scalability
  • DevSecOps: Enhancing Security With Vulnerability Scanning of Images and Source Code in CI/CD
  • Designing High-Performance APIs
  • API and Database Performance Optimization Strategies

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: