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.


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


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:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
    <relativePath/> <!-- lookup parent from repository -->
  <description>Spring Boot Redis Cache Example</description>

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.

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.
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;

public class SpringBootRedisCacheExampleApplication {

  public static void main(String[] args) {, args);
  JedisConnectionFactory jedisConnectionFactory(){
    return new JedisConnectionFactory();

  RedisTemplate<String, Item> redisTemplate(){
    RedisTemplate<String,Item> redisTemplate = new RedisTemplate<String, Item>();
    return redisTemplate;

Creating a Model class:

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


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

    public Item() {

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

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCategory() {
        return category;

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

Creating RestController:

Create 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;

public class ItemController {

    ItemCache itemCache;

    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"})
    public ResponseEntity<Item> addItem(@RequestBody Item item, UriComponentsBuilder builder){
        HttpHeaders headers = new HttpHeaders();
        return new ResponseEntity<Item>(headers, HttpStatus.CREATED);

    public ResponseEntity<Item> updateItem(@RequestBody Item item){
        if(item != null){
        return new ResponseEntity<Item>(item, HttpStatus.OK);

    public ResponseEntity<Void> deleteItem(@PathVariable int id){
        return new ResponseEntity<Void>(HttpStatus.ACCEPTED);

Create Redis Cache implementation:

Creating 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;

public class ItemCache {
    ItemRepository itemRepo;

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

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

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

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.
package com.onlinetutorialspoint.repo;

import com.onlinetutorialspoint.model.Item;
import org.springframework.stereotype.Repository;

import java.util.Map;

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){
    /*delete an item from database*/
    public void deleteItem(int id){

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

Run it:
$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


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).


Download Source from GIT:

Happy Learning 🙂