Spring Boot Cache: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
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">
<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>
</source>
<source lang="properties">
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
</source>
==References==
==References==
* [https://www.mindbowser.com/spring-boot-with-redis-cache-using-annotation/ Spring Boot With Redis Cache Using Annotation]
* [https://www.mindbowser.com/spring-boot-with-redis-cache-using-annotation/ Spring Boot With Redis Cache Using Annotation]
* [https://bitbucket.org/shahedhossain/finology-http-spider/src/master/src/main/java/biz/shahed/finology/http/spider/service/PageTrackerServiceImpl.java Spring Boot With Ehcache Using Annotation]
* [https://bitbucket.org/shahedhossain/finology-http-spider/src/master/src/main/java/biz/shahed/finology/http/spider/service/PageTrackerServiceImpl.java Spring Boot With Ehcache Using Annotation]
* [https://www.baeldung.com/spring-boot-redis-cache Spring Boot Cache with Redis]
* [https://www.baeldung.com/spring-boot-redis-cache Spring Boot Cache with Redis]

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

References