Spring Boot Cache Providers
In this article, I am going to discuss Spring Boot Cache Providers. Please read our previous article where we discussed Spring Boot Caching Examples.
Spring Boot Cache Providers
As we know by now, the Spring Boot framework provides an abstraction layer for implementing caching within an application. This abstraction layer allows us to use an abstract API to access the cache, without having a dependency on a specific cache provider. This means that the business logic can use the abstraction level only, without calling the cache provider’s code directly.
Spring Boot supports several cache providers, including EhCache, Redis, Hazelcast, Infinispan, and Caffeine. If Spring Boot finds a cache provider on the classpath, it tries to find a default configuration for this provider. This allows us to configure caching transparently and explicitly within our application.
Spring Boot simplifies the implementation of caching through its auto-configuration feature. It automatically searches for libraries and configuration files in the classpath and initializes the required beans during application start-up. To auto-configure a cache provider, we follow these steps:
- Add annotation @EnableCaching in the configuration file.
- Add required caching libraries in the classpath.
- In the root of the classpath, add the configuration file for the cache provider.
When we do not define a bean of type CacheManager or CacheResolver, Spring Boot tries to detect any one of the following cache providers:
If Spring Boot finds more than one cache provider, the cache provider explicitly specified in the application.properties file will take priority.
Types of Caching
When at least one cache bean is defined in the spring-context-support module, then Spring Boot uses the Generic cache. The CacheManager bundles all the beans and configures them.
This is a self-starting process provided by javax.cache.spi.CachingProvider package. It comes with the spring-boot-starter-cache dependency. We can add any other cache library as well.
This is a Java-based and open-source cache provider. It is one of the most widely used cache providers. There are two ways to configure EhCache:
- Configuring the Java POJO class where all configuration parameters are configured through EhCache API.
- Adding another XML file (name ehcache.xml) where we configure EhCache according to the provided schema.
When caching is enabled in an application, Spring Boot wraps the HazelcastInstance automatically in the CacheManager. It distributes the data equally among the nodes.
This is an embedded Java library that can be used as a cache or as a data grid. Data is stored in the key-value form. It can be easily integrated with JCache, JPA Quarkus, Spring, etc. There is no default file location, hence it should be specified explicitly. If it is not specified, the default bootstrap is used.
The CouchebaseCacheManager is automatically configured when couchbase-spring-cache is implemented. All the operations related to the cache perform in the Bucket. The customizer allows us to create additional Buckets, in which we can create another cache.
RedisCacheManager is autoconfigured when we configure Redis. We can create additional caches. The default configuration can be done by using the property spring.cache.redis.*. Complete control over the default configuration can be achieved by using the RedisCacheConfiguration bean.
This is a Java-based caching library, which provides an in-memory cache. The spring-boot-starter-cache dependency automatically configures this, if it is found in the classpath. The caffeine cache allows us to define the size and TTL (time to live) of the cache by using the property spring.cache.caffeine.spec.
This is the default implementation. If no cache provider is specified, a ConcurrentHashMap is used as a cache store.
When we enable caching (using the @EnableCaching annotation), Spring Boot expects a suitable configuration. ‘None’ is used when we want to disable the cache in a certain environment. We use the property spring.cache.type in the application.properties file to disable the cache.
In this article, I am going to discuss Spring Boot EhCache with Examples. Here, in this article, I try to explain Spring Boot Cache Providers. I hope you enjoy this Spring Boot Cache Providers article.