![]() |
VOOZH | about |
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.
Follow the steps below to understand how caching works in Spring Boot and how different cache providers can be configured.
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:
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,
To use caching in a Spring Boot application, caching must be enabled using the @EnableCaching annotation.
The @EnableCaching annotation enables Spring Boot’s annotation-driven cache management capability.
To use caching features in Spring Boot, add the following dependency in the pom.xml file.
This dependency enables the caching infrastructure in Spring Boot. If additional caching libraries are required (such as EhCache or Guava), we may also include:
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.
Before invoking the getName() method, Spring checks whether the requested data exists in the Names cache.
This process reduces repeated computation and improves performance.
Spring Boot simplifies caching configuration using auto-configuration.
When caching is enabled:
Below are some commonly used cache providers in Spring Boot.
Generic caching is used when the application defines at least one Cache bean. Spring Boot creates a CacheManager that wraps all these cache beans.
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
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>
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
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.
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
Spring Boot automatically configures CouchbaseCacheManager if Couchbase dependencies are present.
Example configuration:
spring.cache.cache-names=cache1,cache2
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.
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>