Hi there,

if you are looking for integrating redis cache in spring boot application for faster read and write operations then this blog is for you.

First, what is Redis and in-memory database?
When your application scales, your API calls that fetching data from the relational databases like MySQL, Oracle, PostgreSQL etc. slows down as there’s a lot of data to pick from. Here Redis comes into picture, so Redis is:

  • NoSQL database where no SQL (Structured Query Language) is used to store data that is, In redis, there are no relation such as tables and relationship exist between the data.
  • It is in-memory database means, data reside in your main memory which is obviously faster to retrieve than compared to the disk storage.

Now you are saying, hey what it uses to store data? So,

  • It has four data structure: Strings, Sets, Hashes (which is map) and sorted sets (which is a set with score associated with it).
  • Set is a data structure which stores only unique values.
  • Hash is a data structure which stores a key-value pair.
  • sorted set is a set which is sorted with respect to score.

Now, you have clear idea of what redis is and why it is fast enough in terms of scalability, so for the installation click here

No matter what library you are using, you need Redis client to connect to redis. There are two popular clients available out there:

  • Lettuce and
  • Jedis

Use can quickly checkout in what features they differ, in my scenario, lettuce worked fine so I’ll be using spring data redis along with Lettuce client in this blog.

add these dependency to your pom.xml file,
path: src -> resources -> pom.xml

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>io.lettuce</groupId>
    <artifactId>lettuce-core</artifactId>
    <version>5.3.4.RELEASE</version>
 </dependency>

In this spring data redis, we have RedisTemplate interface which takes care of all redis things. It is easy to use, otherwise opening and closing connections in redis is painful, as it single threaded.
Now, lets create beans for RedisTemplate with lettuce client,

    @Bean
    public LettuceConnectionFactory lettuceConn(){
        return new LettuceConnectionFactory(new          RedisStandaloneConfiguration("localhost", 6379));
    }

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

Now your redis server is listening on localhost:6379. You have successfully created bean for redisTemplate which you can @autowired in your repository. Lets take an example by creating a demo repository:

package com.demo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.DefaultTypedTuple;
import org.springframework.stereotype.Repository;

import java.util.*;

@Repository
public class DemoRepository{

    @Autowired
    RedisTemplate redisTemplate;

    public void addItemToSet(String setName, String value){
       redisTemplate.opsForSet().add(setName, value);
    }
    public Set<String> getAllFromSet(String setName){
       return redisTemplate.opsForSet().get(setName);
    }
    public void addItemToHash(String hashName, String hashKey, String value){
       redisTemplate.opsForHash().put(hashName, hashKey, value);
    }
    public Map<String, String> getAllFromHash(String hashName){
       return redisTemplate.opsForHash().entries(hashName);
    }
    public void addItemToZSet(String setName, String value, double score){
       redisTemplate.opsForZSet().add(setName, value, score);
    }
    public Set<DefaultTypedTuple> getItemFromZSet(String setName){
       return redisTemplate.opsForZSet().rangeWithScore(setName, 0, -1);
    }
}

these are the few commands in Redis, you can explore more here.

Hope this help! Happy coding 🙂

Leave a comment

Your email address will not be published. Required fields are marked *