Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default Cache Manager with Spring Boot using @EnableCaching

I have implemented caching in my SpringBootApplication as shown below

@SpringBootApplication
@EnableCaching
public class SampleApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SampleApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

This is absolutely working fine.

But to implement caching there should be one mandatory CacheManager / Cacheprovider defined. Without defining any cacheManager also my application is working fine.

Is there any default Cache manager defined by Spring ? Spring docs says Spring Boot auto-configures a suitable CacheManager.

So what will be CacheManager used if we do not define it ?

like image 520
PRATHAP S Avatar asked Jan 19 '17 10:01

PRATHAP S


People also ask

What is default cache provider in spring boot?

In Spring Boot, the cache provider gives authorization to programmers to configure cache explicitly in an application. It incorporates various cache providers such as EhCache, Redis, Guava, Caffeine, etc. To add caching to an operation of your application we need to add @Cacheable annotation to its method.

What is @EnableCaching in spring boot?

The @EnableCaching annotation triggers a post-processor that inspects every Spring bean for the presence of caching annotations on public methods. If such an annotation is found, a proxy is automatically created to intercept the method call and handle the caching behavior accordingly.

What is cache manager in spring boot?

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.


2 Answers

The Spring Boot starter provides a simple cache provider which stores values in an instance of ConcurrentHashMap. This is the simplest possible thread-safe implementation of the caching mechanism.

If the @EnableCaching annotation is present in your app, Spring Boot checks dependencies available on your class path and configures an appropriate CacheManager. Depending on a chosen provider, some additional configuration may be required. You can find all information about configuration in the first link from this answer.

like image 191
Daniel Olszewski Avatar answered Sep 19 '22 12:09

Daniel Olszewski


If you want to define explicitly (from any reason) the simplest cache manager (which uses ConcurrentHashMap under the hood), please do:

@Bean
public CacheManager cacheManager() {
    return new org.springframework.cache.concurrent.ConcurrentMapCacheManager();
}
like image 28
kasopey Avatar answered Sep 20 '22 12:09

kasopey