Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
Caching in Spring Boot aims to improve the performance of an application by storing the result of expensive computations or frequently accessed data so that subsequent requests can be served faster. Spring's caching abstraction allows you to seamlessly add caching into your application without coupling to any specific cache provider.
Here's how to set up and use caching in a Spring Boot application:
Dependencies:
Include the Spring Boot cache starter dependency in your pom.xml
:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
Enable Caching:
In your main Spring Boot application class or any configuration class, add the @EnableCaching
annotation:
@SpringBootApplication @EnableCaching public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } }
With caching set up, you can use various annotations to define caching behavior:
@Cacheable
: This annotation is used at the method level to indicate that the result of the method should be cached. The first time the method is called, the result will be stored in the cache. Subsequent calls with the same arguments will return the cached value.@Cacheable(value = "books", key = "#isbn") public Book getBookByIsbn(String isbn) { //... fetch the book from a database or another source }
In this example, "books" is the cache's name, and the key
attribute specifies that the ISBN parameter will be used as the cache key.
@CachePut
: Indicates that a method's return value should always be stored in the cache. Useful when updating data and the cache.@CachePut(value = "books", key = "#book.isbn") public Book updateBook(Book book) { //... update the book in a database return book; }
@CacheEvict
: Used to remove data from the cache. It's particularly useful when deleting data.@CacheEvict(value = "books", key = "#isbn") public void deleteBookByIsbn(String isbn) { //... delete the book from a database }
@Caching
: This is a group annotation that lets you combine multiple cache operations. For instance, when a method affects multiple cache names or involves both putting and evicting.@Caching( put = {@CachePut(value = "book", key = "#book.id")}, evict = {@CacheEvict(value = "allBooksCache")} ) public Book saveBook(Book book) { //... save logic return book; }
By default, Spring Boot uses a simple in-memory cache, but you can easily switch to other cache providers like EhCache, Redis, Hazelcast, etc., by adding the appropriate dependencies and, in some cases, some minimal configuration.
You can also define and configure cache managers in Java code. For example:
@Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("books"))); return cacheManager; }
It's important to manage the lifecycle of cached data, especially in scenarios where the underlying data changes or the cache can grow indefinitely. Spring provides:
@CacheEvict
: To remove specific entries or all entries from a cache.You can conditionally apply caching using conditions in your caching annotations:
@Cacheable(value = "books", key = "#isbn", condition = "#isbn != null") public Book getBookByIsbn(String isbn) { //... logic }
In this example, caching is applied only when the ISBN is not null.
Caching is a powerful feature in Spring Boot that can significantly improve application performance. However, it's crucial to manage cached data properly to ensure data consistency and application stability. Proper cache eviction and expiration strategies should be in place based on the application's specific requirements.
Configuring and enabling caching in Spring Boot:
@EnableCaching
.// Enable caching in a Spring Boot application @SpringBootApplication @EnableCaching public class MyApplication { // Application code }
Using @Cacheable annotation in Spring Boot:
@Cacheable
annotation to cache method results based on specified conditions.// Cacheable method in Spring Boot @Cacheable("myCache") public String getData(String key) { // Method logic }
Cache eviction strategies in Spring Boot:
// Evict cache entry after 10 minutes @CacheEvict(value = "myCache", key = "#key", beforeInvocation = false, allEntries = false, condition = "#key.length() < 10") public void evictCache(String key) { // Eviction logic }
Conditional caching in Spring Boot:
@Cacheable
.// Conditional caching based on a property @Cacheable(value = "myCache", condition = "#{T(java.lang.System).getProperty('caching.enabled') == 'true'}") public String getData(String key) { // Cache data if caching is enabled }
In-memory caching with Spring Boot:
pom.xml
dependency):<!-- Caffeine dependency --> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency>Example (configuration):
// Caffeine configuration in Spring Boot @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new CaffeineCacheManager("myCache"); } }
Integrating Ehcache with Spring Boot caching:
pom.xml
dependency):<!-- Ehcache dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> </dependency>Example (configuration):
// Ehcache configuration in Spring Boot @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { return new EhCacheCacheManager(ehCacheCacheManager().getObject()); } @Bean public EhCacheManagerFactoryBean ehCacheCacheManager() { EhCacheManagerFactoryBean factory = new EhCacheManagerFactoryBean(); factory.setConfigLocation(new ClassPathResource("ehcache.xml")); factory.setShared(true); return factory; } }
Redis caching in Spring Boot applications:
pom.xml
dependency):<!-- Redis dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>Example (configuration):
// Redis configuration in Spring Boot @Configuration public class CacheConfig { @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { return RedisCacheManager.builder(redisConnectionFactory).build(); } }