English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot caching providers

SpringBoot framework allows integration with various cache providerFor example EhCache, Redis, Hazelcast, Infinispan, Caffeine,etc. Cache providers allow developers to transparently and explicitly configure caching in applications. We should use caching because it reduces the number of executions and improves the performance of the application.

In Spring, the caching abstraction does not provide the actual cache space. This depends on org.springframework.cache.Cache Or org.springframework.cache.CacheManager abstraction of the interface.

Caching Auto-Configuration

The Spring Boot Framework simplifies the implementation of caching through automatic configuration. It searches for libraries and configuration files in the classpath and initializes the required dependency beans at application startup. The automatic configuration of caching includes the following steps:

Add annotations to the configuration file @EnableCaching . Add the required to the classpathcache library. Add the cache provider to the root directory of the classpathconfiguration file.

For example, if we want to implement EhCache Firstly, we enable caching in the configuration file.

@SpringBootApplication
@EnableCaching
public class Employee
{
@Bean
public CacheManager cacheManager()
{
//some code
}
}

in pom.xml to the file EhCache dependencies. It adds the required libraries to the classpath.

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

Finally, configure the cache provider configuration file. Here, we are using EhCache, so we need to configure it at the root directory of the classpath. ehcache.xml file.

When we have not defined CacheManager Or CacheResolver The Spring Boot Framework will attempt to detect the following cache providers:

Generic JCache EhCache Hazelcast Infinispan Couchbase Redis Caffeine Simple

If Spring Boot finds multiple cache providers on the classpath, in this case, we must in}} application.properties to explicitly specify the cache provider in the

spring.cache.ehcache.provider=net.sf.ehcache.CacheManager
spring.cache.ehcache.config=classpath:config/another-config.xml

We can use the property spring.cache.type settings for specific cache providers. If caching needs to be disabled, use it in specific environments.

spring.cache.type=none

Spring Boot Framework provides a starter dependency that adds basic cache dependencies to the application. By default, the starter cache dependency provides spring-context-support dependency in the pom.xml file.

Note: If we manually add the cache dependency, then we must include the spring-context-support dependency. Because, it provides support for Jcache, EhCache, and Caffeine.
<dependency>
<groupId>org.springframework/groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>

Spring Boot Framework automatically configures CacheManager by implementing CacheManagerCustomizer The interface further customizes it.

In the following example, we set a null value for the main mapping passed.

@Bean
public CacheManagerCustomizer<ConcurrentMapCacheManager> cacheManagerCustomizer() 
{
return new CacheManagerCustomizer<ConcurrentMapCacheManager>() 
{
@Override
public void customize(ConcurrentMapCacheManager cacheManager) 
{
cacheManager.setAllowNullValues(false);
}
};
}

The bean above needs an automatically configured ConcurrentMapCacheManager .If ConcurrentMapCacheManager is not automatically configured, the customizer will not call in any way. We can use any number of custom programs and use annotations @Order Or @Ordered.

Generic cache

If spring-context-support defines at least one org.springframework.cache.Cache.Cache bean, which will use the common cache. CacheManager bundled and all beans are configured. CacheManager bean, which will use the general cache.

JCache

JCache is javax.cache.spi.CachingProvider. It is located in the classpath JSR 107. spring-boot-starter-cache provided JCacheCacheManagerWe can also add any other caching library.

Note: If the cache library provides both native implementation and JSR support, Spring Boot prefers JSR support.

EhCache 2.x

EHCache is an open-source Java-based caching solution that is widely used. To use EhCache, we should use the following dependencies.

<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

There are two ways to configure EhCache:

Firstly, by configuring a Java POJO file (where all configuration parameters are configured through the EhCache API). Secondly, by configuring the XML file, we configure EhCache according to the provided architecture definition within it.

EhCache uses a file named ehcache.xml file. if the application finds the file in the classpath, it is for spring-boot-starter-cache provided EhCacheCacheManager . We can use the following properties to configure the XML file:

spring.cache.ehcache.config=classpath:config/demo-config.xml

Hazelcast

When we enable caching in our application, Spring Boot automatically integrates HazelcastInstance Wrapped in CacheManager. It evenly distributes data between nodes. We can use the following properties to configure Hazelcast.

spring.hazelcast.config=classpath:config/demo-hazelcast.xml

If this property is not set, Spring Boot will try to find it in the classpath. hazelcast.xml Configuration file (Hazelcast configuration).

Infinispan

Infinispan is an embedded Java library. It is used as CacheOr Data grid. It is Key-valueFormally store data. It can easily be integrated with JCache, JPA Quarkus, Spring, and others.

it does not have a default file location, so we should specify it explicitly. If infinispan is not specified explicitly, the default starter will be used.

spring.cache.infinispan.config=infinispan.xml

Couchbase

when we implement couchbase-spring-cache and Couchbase is configured, it will automatically configure CouchebaseCacheManager . All operations related to the cache are in Bucket execute. It allows us to set properties spring.cache.cache-name to create other caches (if needed).

Custom programs allow us to create other buckets where another cache can be created.

Let's use an example to understand the above concepts.

Assuming we need three caches named cacheA , cacheB,and cacheC . cacheA and cacheB are located on the main bucket (i.e., the automatically configured bucket). cacheC is on another bucket that can survive for a few seconds, such as4seconds. Therefore, we can create cacheA and cacheB by specifying the properties as follows:

spring.cache.cache-names=cacheA, cacheB

Redis

When we configure Redis when configured, will automatically configure RedisCacheManager . It also allows us to use the properties spring.cache.cache-names create other caches. You can use the properties spring.cache.redis.* to implement the default configuration.

We can use RedisCacheConfiguration strong>completely control the default configuration.</strong>Bean.

spring.cache.cache-names=cacheA, cacheB
spring.cache.redis.time-to-live=100000

The above properties configure two caches, named cacheA and cacheB, with the following lifespan:10minutes.

Caffeine

Caffeine is a caching library based on Java. It also provides an in-memory cache. If spring-boot-starter-The cache dependency found Caffeine on the classpath and will be automatically configured CaffeineCacheManger If you want to use caffeine in your application, you need to add the following dependencies:

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.7.0</version>
</dependency>

By using Caffeine cache, we can use properties spring.cache.caffeine.spec defines the cache ofSizeandSurvival Time. For example:

spring.cache.cache-names=cacheA,cacheB
spring.cache.caffeine.spec=maximumSize=500,expireAfterAccess=600s

The above configuration creates two caches named cache1and cache2cache. The maximum size of the cache is 500 ,with the maximum survival time of 6 seconds.

Simple

This is the default implementation. If no cache provider is specified. ConcurrentHashMap Configure as cache storage.

For example, if we need two caches, please use setting their names. The following properties:

spring.cache.cache-names=cache1,cache2

None

When we use the annotation @EnableCaching to enable caching, the application needs appropriate configuration. When we want to DisableUse cache when caching. We use properties spring.cache.type Disable Caching.

spring.cache.type=none