Spring Boot provides built-in support for caching using the Spring Cache Abstraction, which allows applications to store frequently accessed data in memory. This helps improve performance by avoiding repeated database queries or external API calls.
- Improves application performance by reducing repeated database access.
- Supports multiple cache providers such as EhCache, Redis, Guava, Caffeine, Hazelcast, and others.
Step-by-Step Implementation
Follow the steps below to understand how caching works in Spring Boot and how different cache providers can be configured.
Step 1: Understanding Spring Boot Cache Abstraction
Spring Boot caching is built on the Spring Cache Abstraction, which provides a consistent way to interact with different caching systems.
The abstraction is based on two main interfaces:
- Cache Interface: Represents the actual cache where data is stored.
- CacheManager Interface: Manages and creates cache instances.
By using these interfaces, developers can easily switch between different cache providers without changing application code.
Note: Caching is commonly used for data that does not change frequently,
Step 2: Enable Caching in Spring Boot
To use caching in a Spring Boot application, caching must be enabled using the @EnableCaching annotation.
package com.gfg.cache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class CacheApplication {
public static void main(String[] args) {
SpringApplication.run(CacheApplication.class, args);
}
}
The @EnableCaching annotation enables Spring Boot’s annotation-driven cache management capability.
Step 3: Add Cache Dependency
To use caching features in Spring Boot, add the following dependency in the pom.xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
This dependency enables the caching infrastructure in Spring Boot. If additional caching libraries are required (such as EhCache or Guava), we may also include:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
Step 4: Using the @Cacheable Annotation
The @Cacheable annotation is used to cache the result of a method. When the method is called again with the same parameters, the cached result is returned instead of executing the method again.
package com.gfg.cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class Student {
@Cacheable("Names")
public String getName(String name) {
System.out.println("Fetching data from database...");
return name;
}
}
How It Works
Before invoking the getName() method, Spring checks whether the requested data exists in the Names cache.
- If data is found in the cache -> the cached result is returned.
- If data is not found -> the method executes and the result is stored in the cache.
This process reduces repeated computation and improves performance.
Step 5: Auto-Configuration of Caching
Spring Boot simplifies caching configuration using auto-configuration.
When caching is enabled:
- Spring Boot scans the classpath for caching libraries.
- It automatically configures the appropriate CacheManager.
- Required cache beans are initialized during application startup.
Common Cache Providers
Below are some commonly used cache providers in Spring Boot.
1. Generic
Generic caching is used when the application defines at least one Cache bean. Spring Boot creates a CacheManager that wraps all these cache beans.
2. JCache (JSR-107)
JCache is a standard Java caching specification. Spring Boot automatically enables it when a CachingProvider is present in the classpath.
Example configuration:
spring.cache.jcache.provider=com.acme.MyCachingProvider
spring.cache.jcache.config=classpath:acme.xml
3. EhCache
EhCache is a widely used Java caching library that improves application performance and scalability.
Example configuration:
spring.cache.ehcache.config=classpath:config/demo-config.xml
Dependency:
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
4. Hazelcast
Hazelcast is an in-memory data platform used for distributed caching and real-time data processing.
Configuration example:
spring.hazelcast.config=classpath:config/demo-hazelcast.xml
5. Guava
Guava provides caching along with several useful Java utilities.
Configuration example:
<bean id="cacheManager"
class="org.springframework.cache.guava.GuavaCacheManager"/>
However, Caffeine is now preferred over Guava for better performance.
6. Infinispan
Infinispan is an open-source in-memory data grid used for distributed caching and large-scale applications.
Configuration example:
spring.cache.infinispan.config=infinispan.xml
7. Couchbase
Spring Boot automatically configures CouchbaseCacheManager if Couchbase dependencies are present.
Example configuration:
spring.cache.cache-names=cache1,cache2
8. Redis
Redis is a high-performance distributed caching system widely used in microservices architectures.
Example configuration:
spring.cache.cache-names=cache1,cache2
spring.cache.redis.time-to-live=60000
This creates two caches with a 1-minute expiration time.
9. Caffeine
Caffeine is a modern high-performance caching library that replaces Guava caching.
Example configuration:
spring.cache.cache-names=cache1,cache2
spring.cache.caffeine.spec=maximumSize=100,expireAfterAccess=60s
Dependency:
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>