JCache is a common API for using caching in Java. Specifies API and semantics for temporary, in memory caching of Java objects, including object creation, shared access, spooling, invalidation, and consistency across JVM’s.

Caching is something that everyone has implemented in there app. There are n numbers of services and system available from just a simple Concurrent Map implementation to all the way to caching servers like Redis and memcache. Each cache provider have distinct api to use. JCache provide a standard set of API to implement caching in your app. JCache is based on JSR-107, JSR was submitted in year 2001 and finally released on 18 Mar 2014.

JCache is just like Jdbc but for caching provider rather then database. Jdbc is comman api interact with any database, JCache is to interact with caching providers.

List of JCache Providers

  • Apache Ignite
  • Blazing Cache
  • cache2k
  • Caffeine
  • Coherence
  • Ehcache
  • Hazelcast
  • Infinispan
  • Redisson
  • Triava Cache
  • WebSphere eXtreme Scale


In this example I have used ehcache as our a caching provider and it is widely used in java world.




package com.dgstack.dev.jcache;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider;

public class JCacheSample {

	public static void main(String[] args) {

		final CachingProvider cachingProvider = Caching.getCachingProvider();

		System.out.println("cachingProvider = " + cachingProvider);

		final CacheManager cacheMgr = cachingProvider.getCacheManager();

		final MutableConfiguration<String, String> configuration = new MutableConfiguration<>();

		configuration.setTypes(String.class, String.class);

		cacheMgr.createCache("sampleCache", configuration);

		final Cache<String, String> sampleCache =
                    cacheMgr.getCache("sampleCache", String.class, String.class);

		sampleCache.put("name", "Digivjay");
		sampleCache.putIfAbsent("name", "Digvijay Bhakuni"); // This will not update the values
		sampleCache.put("jobs", "Developer");

		sampleCache.forEach(e -> {
			System.out.println("(KEY:" + e.getKey() + ")=>[VAL:"+ e.getValue()+"]");



Caching.getCachingProvider() return CacheProvider it check with current classloader that if their is any Cache Provider is loaded, if there is no cache provider then it throws new CacheException("No CachingProviders have been configured") and if more then one provider then it throws new CacheException("Multiple CachingProviders have been configured when only a single CachingProvider is expected").

In our case it will return an instance of Echache Provider because thier is only one cache provider in classpath.

When working with multiple cache providers you can use Caching.getCachingProvider(String name) method take the name. For Echache it would be org.ehcache.jcache.JCacheCachingProvider

Console Output

cachingProvider = [email protected]

This JCache also have support @Annotation and there usage is similar to Spring Caching API. I will post an example explaining usage of jcache annotation base api.

Share :

Subscribe & Social

Subscribe to this blog via RSS.

Recent Posts


Java 14

Python 2

Wordpress 1

Ops 3

Shared 3

Angular 1

Web 1

Popular Tags

Java (14) Python (2) Wordpress (1) Ops (3) Angular (1) Web (1)


DGStack is web blog by digvijay bhakuni. This blog is about computer programming tips and trick. And some short tutorial and gettings started post about the framework, libriay, api and much more. The blog target mostly open source stuff like java, j2ee, nodeJs, angularJs, spring, struts, mongodb, mean, etc


This website claims no credit for any images posted on this site unless otherwise noted. Images on this blog are copyright to its respectful owners. If there is an image appearing on this blog that belongs to you and do not wish for it appear on this site, please E-mail with a link to said image and it will be promptly removed. And the information is best of my knowledge one can always write comment or mail regarding information is this blog.