Spring Boot Cache Provider
The Spring Boot framework allows the integration of various cache providers, such as EhCache, Redis, Hazelcast, Infinispan, Caffeine, etc. The cache provider allows the developer to configure cache transparently and explicitly in an application. We should use cache because it reduces the number of executions and increases the performance of the application.
In Spring Boot, the cache abstraction does not provide the actual space for the cache. It depends on the abstraction that occurred by the org.springframework.cache.Cache or org.springframework.cache.CacheManager interfaces.
The Spring Boot Framework simplifies the implementation of caching by auto-configuration support. It searches for the libraries and configuration-files in the classpath and initializes the required dependency beans at the time of application startup. The auto-configuration of caching includes the following steps:
For example, if we want to implement EhCache in an application, first we enable the cache in the configuration file.
Add EhCache dependency in the pom.xml file. It adds the required libraries in the classpath.
At the end, configure the file for cache provider. Here, we are using the EhCache so need to configure ehcache.xml file at the root of the classpath.
When we do not define a bean of type CacheManager or CacheResolver, the Spring Boot Framework tries to detect the following cache provider:
If the Spring Boot finds the more than one cache provider in the classpath, in such cases, we must specify the cache provider explicitly in the application.properties file.
We can set up a particular cache provider by using the property spring.cache.type. It is used in a certain environment if we want to disable caching.
The Spring Boot Framework provides a starter dependency that adds basic cache dependency in the application. The starter cache dependency, by default, provides the spring-context-support dependency.
Note: We must include spring-context-support dependency in pom.xml file if we add cache dependency manually. Because, it provides support for Jcache, EhCache, and Caffiene.
The Spring Boot Framework automatically configures the CacheManager that can be further customized by implementing the CacheManagerCustomizer interface.
In the follwoig example, we have set up a flag that passes the null values to the primary map.
The above bean expects a auto-configured ConcurrentMapCacheManager. If the ConcurrentMapCacheManager is not auto-configures, the customizer will not invoke in any way. We can have any number of customizer and arrange them in order by using the annotation @Order or @Ordered.
If the spring-context-support defines at least one org.springframework.cache.Cache bean, it uses the Generic cache. The CacheManager bundled all the beans and configured them.
JCache is a self-starting process that is provided by the javax.cache.spi.CahingProvider. It is present on the classpath JSR 107. The spring-boot-starter-cache provides the JCacheCacheManager. We can add any other cache library as well.
Note: Spring Boot prefers the JSR support if a cache library provides both native implementation and JSR support.
EHCache is Java-based, open-source, and widely used cache. In order to use EhCache we should use the following dependency.
There are two ways to configure EhCache:
EhCache used a file called ehcache.xml. If the application found the file on the classpath, the EhCacheCacheManager provided by the spring-boot-starter-cache. We can configure the XML file by using the following property:
When we enable the caching in an application, Spring Boot wraps the HazelcastInstance automatically in the CacheManager. It distributes the data equally among the nodes. We can configure Hazelcast by using the following property.
If the property is not set, Spring Boot tries to find the hazelcast.xml (Hazelcast configuration) file on the classpath.
It does not have a default file location, so we should specify it explicitly. If the infinispan is not specified explicitly, it uses default bootstrap.
The CouchebaseCacheManager is automatically configured when we implement couchbase-spring-cache, and Couchbase is configured. All the operations related to the cache perform in the Bucket. It allows us to create additional caches (if required) by setting up the property spring.cache.cache-name.
The customizer allows us to create additional Buckets, in which we can create another cache.
Let's understand the above concept through an example.
Suppose that we need three caches named cacheA, cacheB, and cacheC. The cacheA and cacheB are on the main Bucket (i.e., auto-configured Bucket). The cacheC is on another Bucket that is to live for a few seconds, say 4 seconds. Hence, we can create cacheA and cacheB by specifying the property, as follows:
The RedisCacheManager is autoconfigured when we configure Redis. It also allows us to create additional cache by using the property spring.cache.cache-names. The default configuration can be achieved by using the property spring.cache.redis.*.
We can take full control over the default configuration by using the RedisCacheConfiguration bean.
The above properties configure two caches named cacheA and cacheB, that lives for 10 minutes.
The caffeine is a Java based caching library. It also provides an in-memory cache. The spring-boot-starter-cache dependency automatically configures the CaffeineCacheManger, if it founds the Caffeine in the classpath. If we want to use Caffeine in an application, we need to add the following dependency:
The caffeine cache allows us to define size and time to live of the cache by using the property spring.cache.caffeine.spec. For example:
The above configuration creates two caches named cache1 and cache2. The maximum size of the cache is 500 and the maximum time to live cache is 6 seconds.
It is the default implementation. If no cache provider is specified. It configures a ConcurrentHashMap as a cache store if the Spring Boot does not find any cache provider in the classpath.
For example, if we want two caches, set their name by using the following property:
When we enable the cache by using the annotation @EnableCaching, the application expects a suitable configuration. It is used when we want to disable the cache in a certain environment. We use the property spring.cache.type to disable the cache.