What is JCache?
If you're a Java software developer who cares about performance and efficiency, you'll almost certainly hear the name "JCache" at some point. But what is JCache exactly, and how does JCache work? In this article, we'll discuss everything you need to know about JCache.
What is JCache?
JCache is the standard caching API (application programming interface) for the Java programming language. Developers can use JCache to temporarily cache Java objects using the CachingProvider interface. JCache is formally known as "JSR107," a Java Specification Request that describes a certain part of the Java platform.
How does JCache work?
The JCache API is located in the javax.cache package. It consists of five core interfaces:
- CachingProvider: The CachingProvider interface oversees the operation of one or more CachingManagers. You can use multiple CachingProviders as necessary.
- CachingManager: The CachingManager interface is responsible for establishing, acquiring, managing, and configuring one or more Caches. In turn, these CachingManagers are controlled by a CachingProvider.
- Cache: The Cache interface is exactly what it sounds like: a cache for temporarily storing Java objects. More specifically, Caches are map data structures in which key-value pairs are stored. Each Cache can only be controlled by one CachingManager.
- Entry: The Entry interface designates a single key-value pair in a Cache.
- ExpiryPolicy: The ExpiryPolicy defines the length of time with which a single Java object is stored in a Cache. Once this time elapses, the object "expires" and can no longer be accessed.
While these five interfaces provide a general structure for JCache APIs, they do not implement it. Instead, any technology that implements a JCache API must adhere to the defined requirements.
Outside these requirements, different JCache implementations can make different assumptions. For example, although JCache requires Caches to use a key-value structure, it does not describe how objects in this format should be stored. One implementation may optimize JCache for space efficiency, while another may decide to lower response times.
This freedom is perhaps most noticeable when JCache implementations choose between the "store by value" and "store by reference" paradigms:
- Store by value: The Cache makes a copy of the object being inserted in the cache, and consults this copy rather than the original object.
- Store by reference: The Cache only holds a link to the original object, which is not stored in the cache. This saves on space requirements, but comes with the risk that the original objects will change during the lifetime of the Cache.
Since JCache is a specification and not an implementation, developers have multiple versions to choose from, including:
- The JCache reference implementation
- Hazelcast
- Terracotta Ehcache
- Oracle Coherence
- Infinispan
Note that the JCache reference implementation is not intended for production use - only as a proof of concept. For example, it lacks features and optimizations such as tiered storage and distributed caching.
JCache and Redis
Redis is an open-source, in-memory data structure store often used to implement key-value NoSQL databases and caches. Although Redis isn't compatible with Java out of the box, Java developers who use Redis can make use of a third-party Redis Java client such as Redisson.
The Redisson client includes an implementation of the JCache API for Redis. Below is a simple demonstration of how to use JCache with Redis and Redisson, loading either a JSON or YAML configuration file:
MutableConfiguration<String, String> config = new MutableConfiguration<>(); // YAML configuration URI redissonConfigUri = getClass().getResource("redisson-jcache.yaml").toURI(); CacheManager manager = Caching.getCachingProvider().getCacheManager(redissonConfigUri, null); Cache<String, String> cache = manager.createCache("namedCache", config);
Redisson passes all of the Technology Compatibility Kit (TCK) tests for JCache, making it a fully certified implementation. Plus, if JCache isn't for you, Redisson also supports two other methods for distributed caching in Java: the Spring framework and the Map collection.