Spring Boot Cache
Jump to navigation
Jump to search
In the save()
method, We have added @CachePut
annotation for updating or adding the particular entity in the cache, and @CacheEvict
is used because If we have created a cache for users list then if the user is updated or deleted then we are going to evict or flush the cache with the given value to makes sure that it should also update in Redis Cache.
In the findById()
method, we have used @Cacheable
annotation with key as id
and value as get/user/
. So that when this method is called, a record of the user with id
will be stored in Redis cache.
Dependencies
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring.cache.redis.cache-null-values: false
spring.cache.redis.time-to-live: 600000
spring.redis.host: 127.0.0.1
spring.redis.port: 6379