Redis 在 SpringBoot 中的使用

3,563 阅读3分钟

Redis 支持多种 java 客户端,支持列表参考官网

Jedis 是redis官方推荐的java客户端

在SpringBoot 中使用 Redis 简单分为三步

  • 引入依赖
  • 配置 redis
  • 使用 redis

引入依赖

springboot 2.0.0.RELEASE 之前,data-redis 底层客户端为 jedis

从springboot 2.0.0.RELEASE 开始底层区分两个不同的实现,jedis及lettuce,默认采用 lettuce

<!-- redis依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

配置

底层 redis 客户端的变更,影响到了上次配置的改变,redis 因此也分为两种

springboot 2.0.0.RELEASE 之前配置

spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
# redis服务器的登录密码 没有可以不配置
#spring.redis.password= 
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
# redis 服务名称
#spring.redis.sentinel.master=
# 主机列表,格式为 host:port, 多个用逗号分隔  
#spring.redis.sentinel.nodes= 
spring.redis.timeout=10

springboot 2.0.0.RELEASE 之后配置

spring.redis.database=0
spring.redis.host=192.168.99.100
spring.redis.port=6379
# redis服务器的登录密码 没有可以不配置
#spring.redis.password= 
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.min-idle=0
# redis 服务名称
#spring.redis.sentinel.master= 
# 主机列表,格式为 host:port, 多个用逗号分隔  
#spring.redis.sentinel.nodes= 
spring.redis.timeout=100ms

主要变动

springboot 2.0.0.RELEASE 之后 pool 改为 lettuce.pool

公共配置 spring.redis.timeout 的参数改为 Duration 类型,需要增加时间单位参数 如毫秒ms 秒s

使用

redis 中 一般有两种数据类型使用最多 String 和 hash,下面以这两种数据类型为例 简单演示下 redis 在 Springboot 中的使用

string数据类型

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class RedisServiceImpl {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加 Redis术语中 string 类型的数据
     *
     * @param key   String类型
     * @param value 任意类型,但必须实现序列化接口
     * @return
     */
    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            // 创建对简单值(Redis术语中的string类型)执行操作的对象
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 添加 Redis术语中 string 类型的数据,并设置超时
     *
     * @param key        String类型
     * @param value      任意类型,但必须实现序列化接口
     * @param expireTime 单位分钟
     * @return
     */
    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            // 创建对简单值(Redis术语中的string类型)执行操作的对象
            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
            operations.set(key, value);
            redisTemplate.expire(key, expireTime, TimeUnit.MINUTES);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 判断 key 是否存在
     *
     * @param key
     * @return
     */
    public boolean existsKey(final String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 根据( Redis 术语中 string 类型的) key 获取值,如果出现异常则返回null
     *
     * @param key
     * @param type
     * @param <T>  存入 redis 时的类型
     * @return
     */
    public <T> T getValue(final String key, Class<T> type) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
        try {
            result = operations.get(key);
            if (result == null) {
                return null;
            }
            // 将 Object 类型强转成 type 对应的类型
            return type.cast(result);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 删除对应的value
     *
     * @param key
     */
    public void removeKey(final String key) {
        // 检查 key 是否存在
        if (existsKey(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            removeKey(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        // 获取所有匹配的键
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys != null && keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }
}

hash 数据类型

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

import java.io.Serializable;
import java.util.Set;

@Service
public class RedisServiceImpl {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 添加 Redis 术语中 hash 类型的数据
     *
     * @param key
     * @param hashKey string 类型
     * @param value   任意类型,但必须实现序列化接口
     * @return
     */
    public boolean setHash(String key, String hashKey, Object value) {
        boolean result = false;
        try {
            // 创建对 hash 值(Redis术语中的 hash 类型)执行操作的对象
            HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
            hashOperations.put(key, hashKey, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 判断redis key中是否有hashKey对应的value
     *
     * @param key
     * @return
     */
    public boolean existsHashKey(String key, String hashKey) {
        return redisTemplate.opsForHash().hasKey(key, hashKey);
    }


    /**
     * 根据key,hashKey读取value
     *
     * @param key
     * @return
     */
    public Object getHashValue(final String key, final String hashKey) {
        // 创建对 hash 值(Redis术语中的 hash 类型)执行操作的对象
        HashOperations<String, String, Object> hashOperations = redisTemplate.opsForHash();
        try {
            return hashOperations.get(key, hashKey);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 判断 key 是否存在
     *
     * @param key
     * @return
     */
    public boolean existsKey(final String key) {
        return redisTemplate.hasKey(key);
    }
    
    /**
     * 删除对应的value
     *
     * @param key
     */
    public void removeKey(final String key) {
        // 检查 key 是否存在
        if (existsKey(key)) {
            redisTemplate.delete(key);
        }
    }

    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            removeKey(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */
    public void removePattern(final String pattern) {
        // 获取所有匹配的键
        Set<Serializable> keys = redisTemplate.keys(pattern);
        if (keys != null && keys.size() > 0) {
            redisTemplate.delete(keys);
        }
    }


    /**
     * 删除对应的key的指定hashKey
     *
     * @param key
     */
    public void removeHashKey(final String key, final String hashKey) {
        if (existsHashKey(key, hashKey)) {
            redisTemplate.opsForHash().delete(key, hashKey);
            ;
        }
    }

}