fwq
2022-12-26 8782bcc0f6673c6b8d6e8e26f3032ac458ad7dd0
src/main/java/com/hx/redis/RedisUtil.java
@@ -1,5 +1,6 @@
package com.hx.redis;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@@ -23,6 +24,18 @@
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    /**
     * 切换数据库
     * @param dataNumber 数据库编号
     */
    public void setDataBase(int dataNumber) {
        LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
        if (connectionFactory != null && dataNumber != connectionFactory.getDatabase()) {
            connectionFactory.setDatabase(dataNumber);
            this.redisTemplate.setConnectionFactory(connectionFactory);
            connectionFactory.resetConnection();
        }
    }
    /**
     * [判断key是否存在]