Spring Boot Cache: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
In the <code>save()</code> method, We have added <code>@CachePut</code> annotation for updating or adding the particular entity in the cache, and <code>@CacheEvict</code> 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 <code>findById()</code> method, we have used <code>@Cacheable</code> annotation with key as <code>id</code> and value as <code>get/user/</code>. So that when this method is called, a record of the user with <code>id</code> will be stored in Redis cache. | |||
==Dependencies== | |||
<source lang="xml"> | <source lang="xml"> | ||
<dependency> | <dependency> |
Latest revision as of 08:11, 16 May 2021
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