Springboot 實現Redis 同數據源動態切換db


默認redis使用的是db 0,而我們自己在配置連接的時候可以設置默認使用db ,如:

 

 

 那么怎么去實現動態 去切換自己想使用的db呢?

LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
connectionFactory.setDatabase(num);

依賴

<!-- Redis相關依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- 如果使用Lettuce作為連接池,需要引入commons-pool2包,否則會報錯bean注入失敗 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.1.0</version>
</dependency>

yml

redis:
host: 192.168.222.157
port: 7002
# password: xatgood
database: 0
timeout: 6000 # 連接超時時間(毫秒)默認是2000ms
lettuce:
pool:
max-active: 200 # 連接池最大連接數(使用負值表示沒有限制)
max-wait: -1ms # 連接池最大阻塞等待時間(使用負值表示沒有限制)
max-idle: 100 # 連接池中的最大空閑連接
min-idle: 50 # 連接池中的最小空閑連接
shutdown-timeout: 500
# sentinel: # 哨兵模式
# master: mymaster
# nodes: 192.168.222.155:26379,192.168.222.155:26380,192.168.222.155:26381
# cluster: #集群模式
# nodes:
# - 192.168.222.157:6379
# - 192.168.222.157:6380
# - 192.168.222.157:6381
# - 192.168.222.157:6389
# - 192.168.222.157:6390
# - 192.168.222.157:6391
max-redirects: 3 # 獲取失敗 最大重定向次數

新建RedisConfig配置類

第一類:

/**
* Redis配置類
*/
@Configuration
public class RedisConfig {

@Resource
private RedisTemplate redisTemplate;

public RedisTemplate setDataBase(int num) {
LettuceConnectionFactory connectionFactory = (LettuceConnectionFactory) redisTemplate.getConnectionFactory();
if (connectionFactory != null && num != connectionFactory.getDatabase()) {
connectionFactory.setDatabase(num);
this.redisTemplate.setConnectionFactory(connectionFactory);
connectionFactory.resetConnection();
connectionFactory.afterPropertiesSet();
}
return redisTemplate;
}
/**
* 序列化注入spring容器的RedisTemplate
*
* @return
*/
@Bean
public RedisTemplate<Serializable, Object> getRedisTemplate() {
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
// mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
//使用StringRedisSerializer來序列化和反序列化redis的key值
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(serializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
// redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}

}

第二類

package com.zdyl.wxapplet.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Redis配置類
 */
@Slf4j
@Configuration
public class RedisConfig {

    @Resource
    private RedisProperties redisProperties;
    @Resource
    private RedisTemplate redisTemplate;


    public static Map<Integer, RedisTemplate<Serializable, Object>> redisTemplateMap = new HashMap<>();

    @PostConstruct
    public void initRedisTemp() throws Exception {
        for (int i = 0; i <= 15; i++) {
            redisTemplateMap.put(i, getRedisTemplate(i));
        }
    }

    /**
     * 獲取redisTemplate實例
     *
     * @param db
     * @return
     */
    private RedisTemplate<Serializable, Object> getRedisTemplate(int db) {
        final RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>();
        LettuceConnectionFactory factory = factory();
        factory.setDatabase(db);
        redisTemplate.setConnectionFactory(factory);
        return serializer(redisTemplate);
    }

    /**
     * redis單機配置
     *
     * @return
     */
    private RedisStandaloneConfiguration redisConfiguration() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(redisProperties.getHost());
        redisStandaloneConfiguration.setPort(redisProperties.getPort());
        //設置密碼
        if (redisProperties.getPassword() != null) {
            redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        }
        return redisStandaloneConfiguration;
    }

    /**
     * redis哨兵配置
     *
     * @return
     */
    private RedisSentinelConfiguration getSentinelConfiguration() {
        RedisProperties.Sentinel sentinel = redisProperties.getSentinel();
        if (sentinel != null) {
            RedisSentinelConfiguration config = new RedisSentinelConfiguration();
            config.setMaster(sentinel.getMaster());
            if (!StringUtils.isEmpty(redisProperties.getPassword())) {
                config.setPassword(RedisPassword.of(redisProperties.getPassword()));
            }
            config.setSentinels(createSentinels(sentinel));
            return config;
        }
        return null;
    }

    /**
     * 獲取哨兵節點
     *
     * @param sentinel
     * @return
     */
    private List<RedisNode> createSentinels(RedisProperties.Sentinel sentinel) {
        List<RedisNode> nodes = new ArrayList<>();
        for (String node : sentinel.getNodes()) {
            String[] parts = StringUtils.split(node, ":");
            Assert.state(parts.length == 2, "redis哨兵地址配置不合法!");
            nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
        }
        return nodes;
    }

    /**
     * redis集群配置
     *
     * @return
     */
    private RedisClusterConfiguration getRedisClusterConfiguration() {
        RedisProperties.Cluster cluster = redisProperties.getCluster();
        if (cluster != null) {
            RedisClusterConfiguration config = new RedisClusterConfiguration();
            config.setClusterNodes(createCluster(cluster));
            if (!StringUtils.isEmpty(redisProperties.getPassword())) {
                config.setPassword(RedisPassword.of(redisProperties.getPassword()));
            }
            config.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
            return config;
        }
        return null;
    }

    /**
     * 獲取集群節點
     *
     * @param cluster
     * @return
     */
    private List<RedisNode> createCluster(RedisProperties.Cluster cluster) {
        List<RedisNode> nodes = new ArrayList<>();
        for (String node : cluster.getNodes()) {
            String[] parts = StringUtils.split(node, ":");
            Assert.state(parts.length == 2, "redis哨兵地址配置不合法!");
            nodes.add(new RedisNode(parts[0], Integer.valueOf(parts[1])));
        }
        return nodes;
    }


    /**
     * 連接池配置
     *
     * @return
     */
    private GenericObjectPoolConfig redisPool() {
        GenericObjectPoolConfig genericObjectPoolConfig =
                new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(redisProperties.getLettuce().getPool().getMaxIdle());
        genericObjectPoolConfig.setMinIdle(redisProperties.getLettuce().getPool().getMinIdle());
        genericObjectPoolConfig.setMaxTotal(redisProperties.getLettuce().getPool().getMaxActive());
        genericObjectPoolConfig.setTestOnBorrow(true);
        genericObjectPoolConfig.setTestWhileIdle(true);
        genericObjectPoolConfig.setTestOnReturn(false);
        genericObjectPoolConfig.setMaxWaitMillis(5000);
        return genericObjectPoolConfig;
    }

    /**
     * redis客戶端配置
     *
     * @return
     */
    private LettuceClientConfiguration clientConfiguration() {
        LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder builder = LettucePoolingClientConfiguration.builder();
        builder.commandTimeout(redisProperties.getLettuce().getShutdownTimeout());
        builder.shutdownTimeout(redisProperties.getLettuce().getShutdownTimeout());
        builder.poolConfig(redisPool());
        LettuceClientConfiguration lettuceClientConfiguration = builder.build();
        return lettuceClientConfiguration;
    }

    /**
     * redis獲取連接工廠
     *
     * @return
     */
    @Scope(scopeName = "prototype")
    private LettuceConnectionFactory factory() {
        //根據配置和客戶端配置創建連接
        LettuceConnectionFactory lettuceConnectionFactory = null;
        if (redisProperties.getSentinel() == null && redisProperties.getCluster() == null) {  //單機模式
            lettuceConnectionFactory = new LettuceConnectionFactory(redisConfiguration(), clientConfiguration());
            lettuceConnectionFactory.afterPropertiesSet();
        } else if (redisProperties.getCluster() == null) {                                      //哨兵模式
            lettuceConnectionFactory = new LettuceConnectionFactory(getSentinelConfiguration(), clientConfiguration());
            lettuceConnectionFactory.afterPropertiesSet();
        } else {                                                                                 //集群模式
            lettuceConnectionFactory = new LettuceConnectionFactory(getRedisClusterConfiguration(), clientConfiguration());
            lettuceConnectionFactory.afterPropertiesSet();
        }
        return lettuceConnectionFactory;
    }

    /**
     * 序列化
     *
     * @param redisTemplate
     * @return
     */
    private RedisTemplate<Serializable, Object> serializer(RedisTemplate redisTemplate) {
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        //使用StringRedisSerializer來序列化和反序列化redis的key值
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
//        redisTemplate.setEnableTransactionSupport(true);
        return redisTemplate;
    }



}

 

Redis工具類

 

package com.zdyl.gateway.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson;
import com.zdyl.gateway.config.RedisConfig;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 單機版redis工具類
 */
@Component
public class RedisUtil {


    @Resource
    private RedisConfig redisConfig;
    private final static Gson gson = new Gson();

    /**
     * 根據db獲取對應的redisTemplate實例
     *
     * @param db
     * @return redisTemplate實例
     */
    public RedisTemplate<Serializable, Object> getRedisTemplateByDb(final int db) {
        return redisConfig.getRedisTemplate(db);
    }


    /**
     * 設置緩存
     *
     * @param key   緩存key
     * @param value 緩存value
     */
    public void setString(String key, String value, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 設置緩存,並且自己指定過期時間
     *
     * @param key
     * @param value
     * @param expireTime 過期時間
     */
    public void setString(String key, String value, int expireTime, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        redisTemplate.opsForValue().set(key, value, expireTime, TimeUnit.SECONDS);
    }


    /**
     * 設置緩存對象,可指定DB
     *
     * @param key 緩存key
     * @param obj 緩存value
     */
    public <T> void setObject(String key, T obj, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        redisTemplate.opsForValue().set(key, obj);
    }

    /**
     * 新增hashMap值
     *
     * @param key
     * @param hashKey
     * @param hashValue
     * @param db
     * @return void
     * @author WangJing
     * @date 2019年10月26日 9:22
     */
    public <T> void hashPutString(Serializable key, Serializable hashKey, String hashValue, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        operations.put(key, hashKey, hashValue);
    }

    /**
     * 以map集合的形式添加鍵值對
     *
     * @param key
     * @param maps
     * @param db
     * @return void
     * @author Sunhj
     * @date 2019年10月26日 9:56
     */
    public void hashPutAll(String key, Map<Serializable, Serializable> maps, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        operations.putAll(key, maps);
    }

    /**
     * 獲取變量中的鍵值對
     * {key3=value3, key1=value1, key5=value5, key4=value4, key2=value2}
     *
     * @param db
     * @param key
     * @return java.util.Map<String, String>
     * @author wangj
     * @date 2019年10月26日 8:47
     */
    public <T> Map<Object, Object> hashGetAll(int db, Serializable key) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * 判斷key是否存在
     *
     * @param key
     * @param db
     * @return java.util.Map<String, String>
     * @author wangj
     * @date 2019年10月26日 8:47
     */
    public <T> Boolean hashHasKey(Serializable key, Serializable hahsKey, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.opsForHash().hasKey(key, hahsKey);
    }

    /**
     * 獲取hash表中存在的所有的鍵
     *
     * @param key
     * @param db
     * @return java.util.List<java.lang.String>
     * @author Sunhj
     * @date 2019年10月26日 10:58
     */
    public Set<Object> hashGetAllHashKeys(Serializable key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.opsForHash().keys(key);
    }

    /**
     * 獲取hash表中存在的所有的值
     *
     * @param key
     * @param db
     * @return java.util.List<java.lang.String>
     * @author Sunhj
     * @date 2019年10月26日 10:58
     */
    public List<Object> hashGetAllHashValues(Serializable key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.opsForHash().values(key);
    }

    /**
     * 根據key,hashKey
     * 獲取hash表中存在的單個值
     *
     * @param key
     * @param db
     * @return java.util.List<java.lang.String>
     * @author Sunhj
     * @date 2019年10月26日 10:58
     */
    public Object hashGetObject(Serializable key, Serializable hashKey, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.opsForHash().get(key, hashKey);
    }

    /**
     * 刪除一個或者多個hash表字段
     *
     * @param key
     * @param db
     * @param fields
     * @return java.lang.Long
     * @author Sunhj
     * @date 2019年10月26日 10:15
     */
    public Long hashDelete(Serializable key, int db, Serializable... fields) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        return operations.delete(key, (Object) fields);
    }

    /**
     * 刪除一個hash表字段
     *
     * @param key
     * @param db
     * @param fields
     * @return java.lang.Long
     * @author Sunhj
     * @date 2019年10月26日 10:15
     */
    public boolean hashDeleteOne(Serializable key, String fields, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        return operations.delete(key, fields) == 1;
    }

    /**
     * 設置緩存對象
     *
     * @param key 緩存key
     * @param obj 緩存value
     */
    public <T> void setObject(String key, T obj, int expireTime, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        redisTemplate.opsForValue().set(key, obj, expireTime, TimeUnit.SECONDS);
    }

    /**
     * 獲取指定key的緩存
     *
     * @param key---JSON.parseObject(value, User.class);
     */
    public String getObject(String key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        Object o = redisTemplate.opsForValue().get(key);
        return o == null ? null : toJson(o);
    }

    /**
     * 判斷當前key值 是否存在
     *
     * @param key
     */
    public boolean hasKey(String key, int db) {

        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.hasKey(key);
    }


    /**
     * 獲取指定key的緩存
     *
     * @param key
     */
    public String getString(String key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        Object o = redisTemplate.opsForValue().get(key);
        String s = o == null ? null : toJson(o);
        return s;
    }


    /**
     * 刪除指定key的緩存
     *
     * @param key
     */
    public void delete(String from, String key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        redisTemplate.delete(key);
    }

    /**
     * @param key
     * @throws
     * @Title: expire
     * @Description: 更新key的失效時間
     */
    public Boolean expire(String key, int seconds, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        return redisTemplate.expire(key, seconds, TimeUnit.SECONDS);
    }

    /**
     * 移除並獲取列表中第一個元素
     *
     * @param key
     * @param db
     * @return void
     * @author sunhj
     * @date 2019年10月26日 14:35
     */
    public String listLeftPop(Serializable key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        Object leftPop = redisTemplate.opsForList().leftPop(key);
        if (leftPop == null) {
            return null;
        }
        return JSON.toJSONString(leftPop);
    }

    /**
     * 移除並獲取列表最后一個元素
     *
     * @param key
     * @param db
     * @return java.lang.Object
     * @author sunhj
     * @date 2019年10月26日 14:40
     */
    public String listRightPop(Serializable key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        ListOperations<Serializable, Object> operations = redisTemplate.opsForList();
        Object x = operations.rightPop(key);
        if (x == null) {
            return null;
        }
        return JSON.toJSONString(x);
    }

    /**
     * 獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值,沒有則返回null。
     *
     * @param key
     * @param field
     * @param db
     * @return T
     * @author Sunhj
     * @date 2019年10月26日 8:41
     */
    public <T> T hashGet(Serializable key, Serializable field, Class<T> t, int db) {
        try {
            RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
            Object o = redisTemplate.opsForHash().get(key, field);
            String s = o == null ? null : toJson(o);
            return s == null ? null : fromJson(s, t);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 獲取變量中的指定map鍵是否有值,如果存在該map鍵則獲取值(String格式),沒有則返回null。
     *
     * @param key
     * @param field
     * @param db
     * @return T
     * @author Sunhj
     * @date 2019年10月26日 8:41
     */
    public String hashGetString(Serializable key, Serializable field, int db) {

        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        try {
            Serializable serializable = operations.get(key, field);
            if (serializable != null) {
                return serializable.toString();
            }
            return null;
        } catch (Exception e) {
            return null;
        }


    }

    /**
     * 獲取變量中的鍵值對 ??
     *
     * @param key
     * @param db
     * @return java.util.Map<String, String>
     * @author Sunhj
     * @date 2019年10月26日 8:47
     */
    public <T> Map<String, T> hashGetAll(Serializable key, Class<T> t, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();

        Map<Serializable, Serializable> x = operations.entries(key);
        Map<String, T> map = new HashMap<>();

        try {
            for (Serializable xa : x.keySet()) {
                String keyValue = x.get(xa).toString();
                map.put(xa.toString(), JSON.parseObject(keyValue, t));
            }
            return map;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 新增hashMap值
     *
     * @param key
     * @param hashKey
     * @param hashValue
     * @param db
     * @return void
     * @author Sunhj
     * @date 2019年10月26日 9:22
     */
    public <T> boolean hashPut(Serializable key, Serializable hashKey, T hashValue, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        operations.put(key, hashKey, JSON.toJSONString(hashValue, SerializerFeature.WriteMapNullValue));
        return true;
    }

    /**
     * 查看hash表中指定字段是否存在
     *
     * @param key
     * @param field
     * @param db
     * @return boolean
     * @author Sunhj
     * @date 2019年10月26日 10:32
     */
    public boolean hashExists(Serializable key, Serializable field, int db) {

        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        HashOperations<Serializable, Serializable, Serializable> operations = redisTemplate.opsForHash();
        return operations.hasKey(key, field);

    }

    /**
     * 存儲在list的頭部,即添加一個就把它放在最前面的索引處
     *
     * @param key
     * @param value
     * @param db
     * @return java.lang.Long
     * @author sunhj
     * @date 2019年10月26日 14:03
     */
    public Long listLeftPush(Serializable key, Serializable value, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        ListOperations<Serializable, Object> operations = redisTemplate.opsForList();
        return operations.leftPush(key, value);
    }

    /**
     * 獲取所有的KEY
     *
     * @param key
     */
    public List<Object> getHashKeys(String key, int db) {
        RedisTemplate<Serializable, Object> redisTemplate = getRedisTemplateByDb(db);
        List<Object> values = redisTemplate.opsForHash().values(key);
        return values;
    }

    /**
     * Object轉成JSON數據
     */
    private String toJson(Object object) {
        if (object instanceof Integer || object instanceof Long || object instanceof Float ||
                object instanceof Double || object instanceof Boolean || object instanceof String) {
            return String.valueOf(object);
        }
        return gson.toJson(object);
    }

    /**
     * JSON數據,轉成Object
     */
    public <T> T fromJson(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }
}

 

ok,最后簡單的切換使用演示:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class ShaobingApplicationTests {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testStringRedisTemplate() throws InterruptedException {
        redisUtils.setObject("1","測試1",1);
        redisUtils.setObject("2","測試2",2);
        redisUtils.setObject("3","測試3",3);
    }
}

 

 ok,該篇教程就暫且到此結束。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM