1.

How to implement Caching in Spring Boot?

Answer»

Caching is a mechanism that helps in REDUCING roundtrip calls to Database, REST service, files, etc. Performance under heavy load is a key feature expected from any modern web and mobile application, hence caching is really vital to enhance the speed of FETCHING data.

Spring Boot provides a STARTER project for caching “spring-boot-starter-cache”, adding this to an application brings in all the dependencies to enable JSR-107 (JCACHE - Java Temporary Caching API) and Spring caching annotations.

In order to enable caching in a Spring Boot application, we need to add @EnableCaching to the required configuration class. This will automatically configure a suitable CacheManager to serve as a provider for the cache.

Example:

@Configuration @EnableCaching public class CachingConfig { @Bean public CacheManager cacheManager() { RETURN new ConcurrentMapCacheManager("ADDRESSES"); } }

Now to enable caching, we need to add a @Cacheable annotation to the methods where we want to cache the data.

@Cacheable("addresses") public String getAddress(Customer customer) {...}


Discussion

No Comment Found