Some simple usage of redis cache in springboot

Time:2021-9-2
1. Import redis dependencies
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>  
2. Configure redis in application.properties
#Redis configuration
#Redis server address
spring.redis.host=127.0.0.1
#Redis server connection port
spring.redis.port=6379
#Redis database index (0 by default)
spring.redis.database=0  
#Maximum number of connections in the connection pool (negative value indicates no limit)
spring.redis.jedis.pool.max-active=50
#Maximum blocking wait time of connection pool (negative value indicates no limit)
spring.redis.jedis.pool.max-wait=3000
#Maximum free connections in the connection pool
spring.redis.jedis.pool.max-idle=20
#Minimum free connections in connection pool
spring.redis.jedis.pool.min-idle=2
#Connection timeout (MS)
spring.redis.timeout=5000
3. Write an encapsulated redis tool class to facilitate simple operation of redis cache
package com.example.demo.Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     *Read cache
     * @param key
     * @return
     */
    public String get(final String key){
        return redisTemplate.opsForValue().get(key);
    }

    /**
     *Write cache
     * @param key
     * @param value
     * @return
     */
    public boolean set(final String key,String value){
        boolean result = false;

        try {
            redisTemplate.opsForValue().set(key,value);
            result = true;
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     *Update cache
     * @param key
     * @param value
     * @return
     */
    public boolean update(final String key,String value){
        boolean result = false;
        try {
            redisTemplate.opsForValue().getAndSet(key,value);
            result = true;
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     *Delete cache
     * @param key
     * @return
     */
    public boolean delete(final String key){
        boolean result = false;
        try {
            redisTemplate.delete(key);
            result = true;
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
}
4. Write a test class to test the operation results

Insert operation:

/**
*Insert a key value pair with key "username" and value "super"
*/
@Test
public void set(){
  redisUtil.set("username","supper");
}

Operation results:

127.0.0.1: 6379> get username
"supper"

Read operation:

/**
*Read the value whose key is "username"
*/
@Test
public void get(){
  System.out.println(redisUtil.get("username"));
}

Operation results:

supper

Update operation:

/**
*Update the value of the key value pair whose key is "username" to "Chen"
*/
@Test
public void update(){
  redisUtil.update("username","chen");
}

Operation results:

127.0.0.1: 6379> get username
"chen"

Delete operation:

/**
*Delete the key value pair whose key is "username"
*/
@Test
public void del(){
  redisUtil.delete("username");
}

Operation results:

127.0.0.1: 6379> get username
(nil)