SoFunction
Updated on 2025-04-22

Detailed explanation of SpringDataRedis usage

Spring Data Redis enables developers to interact with Redis databases more easily and supports different Redis client implementations such asJedisandLettuce

Spring Data Redis will automatically select a client. Usually, Spring Boot uses it by default.LettuceAs a Redis client. You can also choose to useJedis

Steps to use Spring Data Redis

(1) Introduce spring-boot-starter-data-redis dependency

<dependency>
    <!--rediarely-->
    <groupId></groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
    <!--common-pool-->
    <dependency>
    <groupId></groupId>
    <artifactId>commons-pool2</artifactId>
</dependency>

(2) Configuring Redis information

spring:
  data:
    redis:
      host: 192.168.30.130
      port: 6379
      password: xxxxxx
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: 100ms

(3) Inject RedisTemplate

SpringDataRedis is the most important tool class in Spring Data Redis, which encapsulates various operations on Redis and encapsulates operation APIs of different data types into different types.

SpringDataRedis can accept any type of java object and passRedisSerializerConvert them to bytes (byte[]) format that Redis can handle.

This is because Redis itself can only store byte data, and cannot directly store Java objects. Therefore, Spring Data Redis providesAutomatic serialization and deserialization mechanismTo support the storage and reading of Java objects.

@SpringBootTest
class RedisDemoApplicationTests {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    void testString() {
        //Write a String data        ().set("name","Brother Tiger");
        //Get string data        Object name = ().get("name");
        ("name = " + name);
    }

}

RedisTemplate is used by defaultJDKSerializationRedisSerializerto serialize and deserialize objects, but it hasUnreadable,JDK serialized byte streams are binary and are not easy to read or debug manually. If you need to view the data stored in Redis, JDK serialized objects will not be converted directly back to a human-readable format, making debugging and monitoring difficult.

You can customize the serialization method of RedisTemplate. The common practice is that the key uses String serialization (StringRedisSerializer) and the value uses JSON serialization (GenericJackson2JsonRedisSerializer). This method can automatically help us handle JSON serialization and deserialization, but it will take up extra space.

So in order to save space, we do not use the JSON serializer to process the value, but use the String serializer (StringRedisTemplate) uniformly, requiring only the key and value of String type to be stored. When java objects need to be stored, manually serialize the object to JSON, and manually deserialize the read JSON into an object when reading Redis.

@SpringBootTest
class RedisStringTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    
    @Test
    void testString() {
        //Write a String data        ().set("name","Brother Tiger");
        //Get string data        Object name = ().get("name");
        ("name = " + name);
    }
    
    private static final ObjectMapper mapper = new ObjectMapper();
    @Test
    void testSaveUser() throws JsonProcessingException {
        //Create an object        User user = new User("Brother Tiger", 21);
        //Manual serialization        String json = (user);
        //Write data        ().set("user:200",json);
        //Get data        String jsonUser = ().get("user:200");
        //Manual deserialization        User user1 = (jsonUser, );
        ("user1 = " + user1);
    }

    @Test
    void testHash() {
        ().put("user:400","name","Brother Tiger");
        ().put("user:400","age","21");

        Map<Object, Object> entries = ().entries("user:400");
        ("entries = " + entries);
    }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.