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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • A Systematic Approach for Java Software Upgrades

Trending

  • 5 Subtle Indicators Your Development Environment Is Under Siege
  • Testing SingleStore's MCP Server
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • Integration Isn’t a Task — It’s an Architectural Discipline
  1. DZone
  2. Data Engineering
  3. Data
  4. Introduction to JCache JSR 107

Introduction to JCache JSR 107

By 
Bill Digman user avatar
Bill Digman
·
Feb. 13, 13 · Review
Likes (1)
Comment
Save
Tweet
Share
48.5K Views

Join the DZone community and get the full member experience.

Join For Free

Resin has supported caching, session replication (another form of caching), and http proxy caching in cluster environments for over ten years. When you use Resin caching, you are using the same platform that has the speed and scalability of custom services written in C like NginX with the usability of Java, and the industry platform Java EE. JCache JSR 107 is a distributed cache that has a similar interface to the HashMap that you know and love. To be more specific, the Cache object in JCache looks like a java.util.ConncurrentHashMap. In addition, JCache JSR 107 defines integration with CDI (as well as Spring and Guice). You can decorate services with interceptors that apply caching to the services just by defining annotations.

Resin 4 has support for JCache, and JCache support is required for Java EE 7.

Let's look at a small example to see how easy is to get started with JCache.

package hello.world;




import javax.cache.Cache;
import javax.cache.CacheBuilder;
import javax.cache.CacheManager;
import javax.cache.Caching;
...




@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {

Cache cache;

public Cache cache() {
if (cache == null) { //building a cache
CacheManager manager = Caching.getCacheManager("cacheManagerHello");
CacheBuilder builder = manager.createCacheBuilder("a");
cache = builder.build();
}
return cache;
}




protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                                                                       throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().append("



");




String helloMessage = cache().get("hello message");

if (helloMessage == null) {
helloMessage = new StringBuilder(20)
.append("Hello World ! 



")
.append(System.currentTimeMillis()).toString();




cache().put("hello message", helloMessage); // <-------------- putting results in the cache
}




response.getWriter().append(helloMessage);





response.getWriter().append("



");
}




}

The above works out fairly well, but what if we want to periodically change the helloMessage. Let's say we get 2,000 requests a second, but every 10 seconds or so we would like to regenerate the helloMessage.

The message might be:

Hello World ! 
1358979745996

Later we would want it to change.

If we wanted it to change every 10 seconds after it was last accessed, we would do this:

cache = builder.setExpiry(ExpiryType.ACCESSED, new Duration(TimeUnit.SECONDS, 10)).build();

For this example, we want to change it every 10 seconds after is was last modified. We would set up the timeout on the creation as follows:

cache = builder.setExpiry(ExpiryType.MODIFIED, new Duration(TimeUnit.SECONDS, 10)).build();

This would go right in the cache method we defined earlier.

public Cache<String, String> cache() {
if (cache == null) {
CacheManager manager = Caching.getCacheManager("cacheManagerHello");

CacheBuilder<String,String> builder = manager.createCacheBuilder("b");
cache = builder.setExpiry(ExpiryType.MODIFIED, new Duration(TimeUnit.SECONDS, 10)).build();
}
return cache;
}

Resin's JCache implementation is built on top Resin distributed cache architecture. You get replication, and data redundancy built in.

Bill Digman is a Java EE / Servlet enthusiast and Open Source enthusiast who loves working with Caucho's Resin Servlet Container, a Java EE Web Profile Servlet Container.

Caucho's Resin OpenSource Servlet Container

Java EE Web Profile Servlet Container

Caucho's Resin 4.0 JCache blog post

Java EE Cache (computing) Resin (software)

Opinions expressed by DZone contributors are their own.

Related

  • Scaling DevOps With NGINX Caching: Reducing Latency and Backend Load
  • Develop a Reverse Proxy With Caching in Go
  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • A Systematic Approach for Java Software Upgrades

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • 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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!