VOOZH about

URL: https://dzone.com/articles/sneak-peek-jcache-api-jsr-107

⇱ Sneak Peek into the JCache API (JSR 107)


Related

  1. DZone
  2. Coding
  3. Java
  4. Sneak Peek into the JCache API (JSR 107)

Sneak Peek into the JCache API (JSR 107)

Likes
Comment
Save
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

This post covers the JCache API at a high level and provides a teaser – just enough for you to (hopefully) start itching about it ;-)

In this post ….

  • JCache overview
  • JCache API, implementations
  • Supported (Java) platforms for JCache API
  • Quick look at Oracle Coherence
  • Fun stuff – Project Headlands (RESTified JCache by Adam Bien) , JCache related talks at Java One 2014, links to resources for learning more about JCache

What is JCache?

JCache (JSR 107) is a standard caching API for Java. It provides an API for applications to be able to create and work with in-memory cache of objects. Benefits are obvious – one does not need to concentrate on the finer details of implementing the Caching and time is better spent on the core business logic of the application.

JCache components

The specification itself is very compact and surprisingly intuitive. The API defines high level components (interfaces) some of which are listed below

  • Caching Provider β€“ used to control Caching Managers and can deal with several of them,
  • Cache Manager β€“ deals with create, read, destroy operations on a Cache
  • Cache β€“ stores entries (the actual data) and exposes CRUD interfaces to deal with the entries
  • Entry β€“ abstraction on top of a key-value pair akin to a java.util.Map
πŸ‘ jcache-high-level-components

Hierarchy of JCache API components

JCache Implementations

JCache defines the interfaces which of course are implemented by different vendors a.k.a Providers.

From the application point of view, all that’s required is the implementation to be present in the classpath. The API also provides a way to further fine tune the properties specific to your provider via standard mechanisms.

You should be able to track the list of JCache reference implementations from the JCP website link

public class JCacheUsage{
 public static void main(String[] args){
 //bootstrap the JCache Provider
 CachingProvider jcacheProvider = Caching.getCachingProvider();
 CacheManager jcacheManager = jcacheProvider.getCacheManager();
 //configure cache
 MutableConfiguration<String, Integer> jcacheConfig = new MutableConfiguration<>();
 jcacheConfig.setTypes(String.class, MyPreciousObject.class);
 //create cache
 Cache<String, MyPreciousObject> cache = jcacheManager.createCache("PreciousObjectCache", jcacheConfig);
 //play around
 String key = UUID.randomUUID().toString();
 cache.put(key, new MyPreciousObject());
 MyPreciousObject inserted = cache.get(key);
 cache.remove(key);
 cache.get(key); //will throw javax.cache.CacheException since the key does not exist
 }
}

JCache provider detection

  • JCache provider detection happens automatically when you only have a single JCache provider on the class path
  • You can choose from the below options as well
//set JMV level system property
-Djavax.cache.spi.cachingprovider=org.ehcache.jcache.JCacheCachingProvider
 
//code level config
System.setProperty("javax.cache.spi.cachingprovider","org.ehcache.jcache.JCacheCachingProvider
 
//you want to choose from multiple JCache providers at runtime
CachingProvider ehcacheJCacheProvider = Caching.getCachingProvider("org.ehcache.jcache.JCacheCachingProvider");
 
//which JCache providers do I have on the classpath?
Iterable<CachingProvider> jcacheProviders = Caching.getCachingProviders();

Java Platform support

  • Compliant with Java SE 6 and above
  • Does not define any details in terms of Java EE integration. This does not mean that it cannot be used in a Java EE environment – it’s just not standardized yet.
  • Could not be plugged into Java EE 7 as a tried and tested standard
  • Candidate for Java EE 8

Project Headlands: Java EE and JCache in tandem

  • By none other than Adam Bien himself !
  • Java EE 7, Java SE 8 and JCache in action
  • Exposes the JCache API via JAX-RS (REST)
  • Uses Hazelcast as the JCache provider
  • Highly recommended !

Oracle Coherence

This post deals with high level stuff w.r.t JCache in general. However, a few lines about Oracle Coherence in general would help put things in perspective

πŸ‘ coherence-logo

  • Oracle Coherence is a part of Oracle’s Cloud Application Foundation stack
  • It is primarily an in-memory data grid solution
  • Geared towards making applications more scalable in general
  • What’s important to know is that from version 12.1.3 onwards, Oracle Coherence includes a reference implementation for JCache (more in the next section)

JCache support in Oracle Coherence

  • Support for JCache implies that applications can now use a standard API to access the capabilities of Oracle Coherence
  • This is made possible by Coherence by simply providing an abstraction over its existing interfaces (NamedCache etc). Application deals with a standard interface (JCache API) and the calls to the API are delegated to the existing Coherence core library implementation
  • Support for JCache API also means that one does not need to use Coherence specific APIs in the application resulting in vendor neutral code which equals portability
    How ironic – supporting a standard API and always keeping your competitors in the hunt ;-) But hey! That’s what healthy competition and quality software is all about !
  • Talking of healthy competition – Oracle Coherence does support a host of other features in addition to the standard JCache related capabilities.
  • The Oracle Coherence distribution contains all the libraries for working with the JCache implementation

πŸ‘ coherence_lib

  • The service definition file in the coherence-jcache.jar qualifies it as a valid JCache provider implementation

πŸ‘ service-definition

Curious about Oracle Coherence ?

JCache at Java One 2014

Couple of great talks revolving around JCache at Java One 2014

Hope this was fun :-)

Cheers !

API Oracle Coherence Coherence (units of measurement) Java EE Java (programming language) application Implementation Cache (computing) Peek (software)

Published at DZone with permission of Abhishek Gupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Jakarta EE Glossary: The Terms Every Java Engineer Should Actually Understand
  • A Systematic Approach for Java Software Upgrades
  • Implementation Best Practices: Microservice API With Spring Boot
  • The Next Evolution of Java: Faster Innovation, Simpler Adoption

Partner Resources

Γ—

Comments

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

Let's be friends: