Spring Boot Redis Cache Example | Redis @Cachable @Cacheput

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to see how to work with Spring Boot Redis Cache using Redis Server as backend.
In our previous tutorials, we have seen how to work with Spring Boot Data Redis where we implemented all CRUD operations on Redis. If you are not familiar with Redis CRUD operations, I recommend you to go through once.
This Redis Cache example goes on top of the CRUD operations.
Spring Boot 2.1.4
Spring Boot Redis Cache
Redis server 2.4.5
Jedis
Java8
Maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.onlinetutorialspoint</groupId>
  <artifactId>Spring-Boot-Redis-Cache-Example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Spring-Boot-Redis-Cache-Example</name>
  <description>Spring Boot Redis Cache Example</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.3</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

Making spring boot application server port as 8082 and defining spring cache type as redis. This is recommended because spring supports different cache implementations like EhCache, HazelCastCache, etc.
spring.cache.type=redis
server.port=8082
As part of this example, I am going to save data into my local redis server, and the data should be cached into redis cache whenever we get it from the server initially.
To connect with redis server (for me it is in local, and it can be remote server also), we have to make the JedisTemplate (it is something similar like JdbcTemplate) through the JedisConnectionFactory object.
package com.onlinetutorialspoint;

import com.onlinetutorialspoint.model.Item;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

@SpringBootApplication
@EnableCaching
public class SpringBootRedisCacheExampleApplication {

  public static void main(String[] args) {
    SpringApplication.run(SpringBootRedisCacheExampleApplication.class, args);
  }
  @Bean
  JedisConnectionFactory jedisConnectionFactory(){
    return new JedisConnectionFactory();
  }

  @Bean
  RedisTemplate<String, Item> redisTemplate(){
    RedisTemplate<String,Item> redisTemplate = new RedisTemplate<String, Item>();
    redisTemplate.setConnectionFactory(jedisConnectionFactory());
    return redisTemplate;
  }
}

Creating Item.class which will act as a JSON message to save in Redis server.
package com.onlinetutorialspoint.model;

import java.io.Serializable;

public class Item implements Serializable {
    private int id;
    private String name;
    private String category;

    public Item() {
    }

    public Item(int id, String name, String category) {
        this.id = id;
        this.name = name;
        this.category = category;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}

Create ItemController.java it provides essential CRUD endpoints. There is nothing unusual in this class.
package com.onlinetutorialspoint.controller;

import com.onlinetutorialspoint.cache.ItemCache;
import com.onlinetutorialspoint.model.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponentsBuilder;

@RestController
public class ItemController {

    @Autowired
    ItemCache itemCache;

    @GetMapping("/item/{itemId}")
    @ResponseBody
    public ResponseEntity<Item> getItem(@PathVariable int itemId){
        Item item = itemCache.getItem(itemId);
        return new ResponseEntity<Item>(item, HttpStatus.OK);
    }

    @PostMapping(value = "/addItem",consumes = {"application/json"},produces = {"application/json"})
    @ResponseBody
    public ResponseEntity<Item> addItem(@RequestBody Item item, UriComponentsBuilder builder){
        itemCache.addItem(item);
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(builder.path("/addItem/{id}").buildAndExpand(item.getId()).toUri());
        return new ResponseEntity<Item>(headers, HttpStatus.CREATED);
    }

    @PutMapping("/updateItem")
    @ResponseBody
    public ResponseEntity<Item> updateItem(@RequestBody Item item){
        if(item != null){
            itemCache.updateItem(item);
        }
        return new ResponseEntity<Item>(item, HttpStatus.OK);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseEntity<Void> deleteItem(@PathVariable int id){
        itemCache.deleteItem(id);
        return new ResponseEntity<Void>(HttpStatus.ACCEPTED);
    }
}

Creating ItemCache.java which has all the Spring cache components. For more details on Spring Cache Abstraction, please refer this document.
package com.onlinetutorialspoint.cache;

import com.onlinetutorialspoint.model.Item;
import com.onlinetutorialspoint.repo.ItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

@Component
public class ItemCache {
    @Autowired
    ItemRepository itemRepo;

    @Cacheable(value="itemCache", key="#id")
    public Item getItem(int id){
        System.out.println("In getItem cache Component..");
        Item item = null;
        try{
            item = itemRepo.getItem(id);
        }catch(Exception e){
            e.printStackTrace();
        }
        return item;
    }
    @CacheEvict(value="itemCache",key = "#id")
    public void deleteItem(int id){
        System.out.println("In deleteItem cache Component..");
        itemRepo.deleteItem(id);
    }

    @CachePut(value="itemCache",key = "#id")
    public void addItem(Item item){
        System.out.println("In addItem cache component..");
        itemRepo.addItem(item);
    }

    @CachePut(value="itemCache",key = "#id",condition = "#result != null")
    public void updateItem(Item item){
        System.out.println("In UpdateItem cache Component..");
        itemRepo.updateItem(item);
    }
}

Creating spring boot data operations. Refer our previous tutorial on spring data CRUD operations example to get more details on this.
package com.onlinetutorialspoint.repo;

import com.onlinetutorialspoint.model.Item;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class ItemRepository {

    public static final String KEY = "ITEM";
    private RedisTemplate<String, Item> redisTemplate;
    private HashOperations hashOperations;

    public ItemRepository(RedisTemplate<String, Item> redisTemplate) {
        this.redisTemplate = redisTemplate;
        hashOperations = redisTemplate.opsForHash();
    }

    /*Getting a specific item by item id from table*/
    public Item getItem(int itemId){
        return (Item) hashOperations.get(KEY,itemId);
    }

    /*Adding an item into redis database*/
    public void addItem(Item item){
        hashOperations.put(KEY,item.getId(),item);
    }
    /*delete an item from database*/
    public void deleteItem(int id){
        hashOperations.delete(KEY,id);
    }

    /*update an item from database*/
    public void updateItem(Item item){
        addItem(item);
    }
}

$mvn clean install
$mvn spring-boot:run
[INFO] --- spring-boot-maven-plugin:2.1.4.RELEASE:run (default-cli) @ Spring-Boot-Redis-Cache-Example ---

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-21 21:33:34.057  INFO 7372 --- [           main] o.SpringBootRedisCacheExampleApplication : Starting SpringBootRedisCacheExampleApplication on DESKTOP-RN4SMHT with PID 7372 (D:\w
ork\Spring-Boot-Redis-Cache-Example\target\classes started by Lenovo in D:\work\Spring-Boot-Redis-Cache-Example)
2019-04-21 21:33:34.066  INFO 7372 --- [           main] o.SpringBootRedisCacheExampleApplication : No active profile set, falling back to default profiles: default
2019-04-21 21:33:35.645  INFO 7372 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-04-21 21:33:35.650  INFO 7372 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-04-21 21:33:35.712  INFO 7372 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33ms. Found 0 repository interfaces.
2019-04-21 21:33:37.105  INFO 7372 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2019-04-21 21:33:37.166  INFO 7372 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-21 21:33:37.167  INFO 7372 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-21 21:33:37.448  INFO 7372 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-21 21:33:37.448  INFO 7372 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3197 ms
2019-04-21 21:33:38.772  INFO 7372 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-21 21:33:40.198  INFO 7372 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2019-04-21 21:33:40.204  INFO 7372 --- [           main] o.SpringBootRedisCacheExampleApplication : Started SpringBootRedisCacheExampleApplication in 7.09 seconds (JVM running for 14.688
Make sure you have to start the Redis server and run the redis-cli.exe file to monitor the requests.
Adding an Item to Redis DB:
Redis-cli.exe – Monitor
We can see the logs while inserting an item into the redis server.
Getting the same Item from redis server:
This item should be taken from redis as this is a first time and this should be stored in redis cache
For the next subsequent calls, this item should be taken from redis cache. So reread the same thing and let’s check the logs.
This call should take the item from redis cache.
On the above log statements, we can say that the data were coming from redis cache (As it is a simple GET operation).
Spring Cache Reference
Spring Boot Redis Data
Happy Learning 🙂

Spring Boot Kafka Producer Example
Spring Boot Kafka Consume JSON Messages Example
How to set Spring Boot SetTimeZone
Sending Spring Boot Kafka JSON Message to Kafka Topic
Spring Boot RabbitMQ Message Publishing Example
How To Change Spring Boot Context Path
Spring Boot DataRest Example RepositoryRestResource
Setup/Install Redis Server on Windows 10
Spring Boot Hazelcast Cache Example
Spring Boot Redis Data Example CRUD Operations
MicroServices Spring Boot Eureka Server Example
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot EhCache Example
Spring Boot MongoDB + Spring Data Example
Spring Boot MVC Example Tutorials

Spring Boot Kafka Producer Example
Spring Boot Kafka Consume JSON Messages Example
How to set Spring Boot SetTimeZone
Sending Spring Boot Kafka JSON Message to Kafka Topic
Spring Boot RabbitMQ Message Publishing Example
How To Change Spring Boot Context Path
Spring Boot DataRest Example RepositoryRestResource
Setup/Install Redis Server on Windows 10
Spring Boot Hazelcast Cache Example
Spring Boot Redis Data Example CRUD Operations
MicroServices Spring Boot Eureka Server Example
Spring Boot How to change the Tomcat to Jetty Server
Spring Boot EhCache Example
Spring Boot MongoDB + Spring Data Example
Spring Boot MVC Example Tutorials






Soleil
September 13, 2019 at 1:54 pm - Reply 

Hi, could you give a tutorial or the CacheManager java config for cache a list to redis? Thanks









Fernando
October 17, 2019 at 8:56 am - Reply 

Hi, I’m testing the demo, but the getItem does not work. It seems like the ItemRepo was not injected.










Soleil
September 13, 2019 at 1:54 pm - Reply 

Hi, could you give a tutorial or the CacheManager java config for cache a list to redis? Thanks




Hi, could you give a tutorial or the CacheManager java config for cache a list to redis? Thanks





Fernando
October 17, 2019 at 8:56 am - Reply 

Hi, I’m testing the demo, but the getItem does not work. It seems like the ItemRepo was not injected.




Hi, I’m testing the demo, but the getItem does not work. It seems like the ItemRepo was not injected.
 


Δ
 Spring Boot – Hello World
 Spring Boot – MVC Example
 Spring Boot- Change Context Path
 Spring Boot – Change Tomcat Port Number
 Spring Boot – Change Tomcat to Jetty Server
 Spring Boot – Tomcat session timeout
 Spring Boot – Enable Random Port
 Spring Boot – Properties File
 Spring Boot – Beans Lazy Loading
 Spring Boot – Set Favicon image
 Spring Boot – Set Custom Banner
 Spring Boot – Set Application TimeZone
 Spring Boot – Send Mail
 Spring Boot – FileUpload Ajax
 Spring Boot – Actuator
 Spring Boot – Actuator Database Health Check
 Spring Boot – Swagger
 Spring Boot – Enable CORS
 Spring Boot – External Apache ActiveMQ Setup
 Spring Boot – Inmemory Apache ActiveMq
 Spring Boot – Scheduler Job
 Spring Boot – Exception Handling
 Spring Boot – Hibernate CRUD
 Spring Boot – JPA Integration CRUD
 Spring Boot – JPA DataRest CRUD
 Spring Boot – JdbcTemplate CRUD
 Spring Boot – Multiple Data Sources Config
 Spring Boot – JNDI Configuration
 Spring Boot – H2 Database CRUD
 Spring Boot – MongoDB CRUD
 Spring Boot – Redis Data CRUD
 Spring Boot – MVC Login Form Validation
 Spring Boot – Custom Error Pages
 Spring Boot – iText PDF
 Spring Boot – Enable SSL (HTTPs)
 Spring Boot – Basic Authentication
 Spring Boot – In Memory Basic Authentication
 Spring Boot – Security MySQL Database Integration
 Spring Boot – Redis Cache – Redis Server
 Spring Boot – Hazelcast Cache
 Spring Boot – EhCache
 Spring Boot – Kafka Producer
 Spring Boot – Kafka Consumer
 Spring Boot – Kafka JSON Message to Kafka Topic
 Spring Boot – RabbitMQ Publisher
 Spring Boot – RabbitMQ Consumer
 Spring Boot – SOAP Consumer
 Spring Boot – Soap WebServices
 Spring Boot – Batch Csv to Database
 Spring Boot – Eureka Server
 Spring Boot – MockMvc JUnit
 Spring Boot – Docker Deployment