In this tutorial, we are going to see how to work with Spring Boot Redis Cache using Redis Server as backend.

Spring Boot Redis Cache:

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.

Technologies:

  • Spring Boot 2.1.4
  • Spring Boot Redis Cache
  • Redis server 2.4.5
  • Jedis
  • Java8
  • Maven

Prerequisites:

To run the following example in your local machine, you have to install the redis server and make sure of running on your computer.

Spring Boot Redis Cache Example:

Project Structure:

Spring Boot Redis Cache Example-min

Project Dependencies:

pom.xml
<?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>

Application Configurations:

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.

application.properties
spring.cache.type=redis
server.port=8082

Spring Boot Redis Cache Configurations:

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.

SpringBootRedisCacheExampleApplication.java
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 a Model class:

Creating Item.class which will act as a JSON message to save in Redis server.

Item.java
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;
    }
}

Creating RestController:

Create ItemController.java it provides essential CRUD endpoints. There is nothing unusual in this class.

ItemController.java
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);
    }
}

Create Redis Cache implementation:

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

ItemCache.java
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);
    }
}

Create Redis Data operations:

Creating spring boot data operations. Refer our previous tutorial on spring data CRUD operations example to get more details on this.

ItemRepository.java
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);
    }
}

Run it:

Terminal.java
$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

Accessing the application using Postman:

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:

Spring Boot Redis Cache Example 2-min

Redis-cli.exeMonitor

We can see the logs while inserting an item into the redis server.

Spring Boot Redis Cache Example 3-min

Getting the same Item from redis server:

Spring Boot Redis Cache Example 4-min

This item should be taken from redis as this is a first time and this should be stored in redis cache

Spring Boot Redis Cache Example 5-min

For the next subsequent calls, this item should be taken from redis cache. So reread the same thing and let’s check the logs.

Spring Boot Redis Cache Example 6-min

This call should take the item from redis cache.

Spring Boot Redis Cache Example 7-min

On the above log statements, we can say that the data were coming from redis cache (As it is a simple GET operation).

References:

Download Source from GIT:

Happy Learning 🙂