Springboot redis/redisson 根據(jù)系統(tǒng)運(yùn)行環(huán)境自動(dòng)切換集群/單機(jī)狀態(tài)

本文章適用于測(cè)試開(kāi)發(fā)環(huán)境是單機(jī)redis腿椎,生產(chǎn)環(huán)境是集群redis
使用到的jar包

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

    <!-- https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter -->
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.redisson</groupId>
                    <artifactId>redisson-spring-data-23</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-data-21</artifactId>
            <version>3.13.6</version>
        </dependency>

公共使用到的配置類 -JedisProperties

package com.property.framework.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @className: RedisProperties
 * @program: property-new-admin
 * @description: redis配置
 * @author: buliu
 * @create: 2022-03-11 13:48
 */
@Data 
@Configuration
@Component
@ConfigurationProperties(prefix = "spring.redis.jedis.pool")
public class JedisProperties {

    private Integer maxIdle;

    private Integer maxWait;

    private Integer minIdle;

    private Integer maxActive;

}

公共使用到的配置類 -RedisProperties

package com.property.framework.config.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @className: RedisProperties
 * @program: property-new-admin
 * @description: redis配置
 * @author: buliu
 * @create: 2022-03-11 13:48
 */
@Data
@Configuration
@Component
@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

    private String host;

    private Integer port;

    private String password;

    private Integer database; // 這里要注意 貌似是說(shuō)redis集群環(huán)境中不支持指定數(shù)據(jù)庫(kù)名 這個(gè)不知道是不是這樣

    private int timeout;

    private Cluster cluster;

    public static class Cluster {
        private List<String> nodes;

        private Integer maxRedirects;

        public List<String> getNodes() {
            return nodes;
        }

        public void setNodes(List<String> nodes) {
            this.nodes = nodes;
        }

        public Integer getMaxRedirects() {
            return maxRedirects;
        }

        public void setMaxRedirects(Integer maxRedirects) {
            this.maxRedirects = maxRedirects;
        }
    }


}

yml 文件片段(截裙饧凇)

- 1婚夫、dev 環(huán)境-使用的是 jedis

# 當(dāng)前為 dev 環(huán)境
# 數(shù)據(jù)源配置 
# 單機(jī)版
spring:
  redis:
    database: 0
    host: XX.XX.XX.XX
    port: 6379
    password: XXXXXXXX # 密碼(默認(rèn)為空)
    timeout: 6000  # 連接超時(shí)時(shí)長(zhǎng)(毫秒)
    jedis:
      pool:
        max-active: 1000  # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
        max-wait: -1      # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
        max-idle: 10      # 連接池中的最大空閑連接
        min-idle: 5       # 連接池中的最小空閑連接

2抒痒、 prod 環(huán)境-使用的是 jedis

# 當(dāng)前為 dev 環(huán)境
# 數(shù)據(jù)源配置 
# 集群版
spring:
  redis:
    database: 0
    password: XXXXXXXX# 密碼(默認(rèn)為空)
    timeout: 6000  # 連接超時(shí)時(shí)長(zhǎng)(毫秒)
    cluster:
      nodes: XXXXXXXX:7001,XXXXXXXX:7002,XXXXXXXX:7003,XXXXXXXX:7004,XXXXXXXX:7005,XXXXXXXX:7006
      maxRedirects: 2  # 獲取失敗 最大重定向次數(shù)
    jedis:
      pool:
        maxActive: 1000  # 連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
        maxWait: -1      # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
        maxIdle: 10      # 連接池中的最大空閑連接
        minIdle: 5       # 連接池中的最小空閑連接

redis 的配置類

package com.property.framework.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.property.framework.config.properties.JedisProperties;
import com.property.framework.config.properties.RedisProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.Serializable;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Autowired
    private RedisProperties redisProperties;

    @Autowired
    private JedisProperties jedisProperties;


    @Profile("dev") // 指的是在dev環(huán)境下注入bean
    @Bean
    public JedisPool redisPoolFactory() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(jedisProperties.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(jedisProperties.getMaxWait());
        if (StringUtils.isNotBlank(redisProperties.getPassword())) {
            return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), redisProperties.getPassword(), redisProperties.getDatabase());
        } else {
            return new JedisPool(jedisPoolConfig, redisProperties.getHost(), redisProperties.getPort(), redisProperties.getTimeout(), null, redisProperties.getDatabase());
        }
    }

    @Profile("dev") // 指的是在dev環(huán)境下注入bean
    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(redisProperties.getHost());
        redisStandaloneConfiguration.setPort(redisProperties.getPort());
        redisStandaloneConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        redisStandaloneConfiguration.setDatabase(redisProperties.getDatabase());

        JedisClientConfiguration.JedisClientConfigurationBuilder jedisClientConfiguration = JedisClientConfiguration.builder();
        jedisClientConfiguration.connectTimeout(Duration.ofMillis(redisProperties.getTimeout()));
        jedisClientConfiguration.usePooling();
        return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration.build());
    }

    @Profile("prod") // 指的是在prod 環(huán)境下注入bean
    @Bean
    public JedisCluster getJedisCluster() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(jedisProperties.getMaxIdle());
        poolConfig.setMaxWaitMillis(jedisProperties.getMaxWait());
        poolConfig.setMinIdle(jedisProperties.getMinIdle());
        poolConfig.setMaxTotal(jedisProperties.getMaxActive());
        // 截取集群節(jié)點(diǎn)
        String[] cluster = redisProperties.getCluster().getNodes().toArray(new String[0]);
        // 創(chuàng)建set集合
        Set<HostAndPort> nodes = new HashSet<>();
        // 循環(huán)數(shù)組把集群節(jié)點(diǎn)添加到set集合中
        for (String node : cluster) {
            String[] host = node.split(":");
            //添加集群節(jié)點(diǎn)
            nodes.add(new HostAndPort(host[0], Integer.parseInt(host[1])));
        }
        //需要密碼連接的創(chuàng)建對(duì)象方式
        return new JedisCluster(nodes, redisProperties.getTimeout(), 1000, redisProperties.getCluster().getMaxRedirects(), redisProperties.getPassword(), poolConfig);
    }

    @Profile("prod") // 指的是在prod 環(huán)境下注入bean
    @Bean
    public RedisClusterConfiguration getClusterConfig() {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(redisProperties.getCluster().getNodes());
        redisClusterConfiguration.setMaxRedirects(redisProperties.getCluster().getMaxRedirects());
        redisClusterConfiguration.setPassword(RedisPassword.of(redisProperties.getPassword()));
        return redisClusterConfiguration;
    }

    @Profile("prod") // 指的是在prod 環(huán)境下注入bean
    @Bean
    public JedisConnectionFactory redisConnectionFactory(RedisClusterConfiguration cluster) {
        return new JedisConnectionFactory(cluster);
    }

    @Bean(name = "redisTemplate")
    @SuppressWarnings({"rawtypes"})
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        //使用 fastjson 序列化
        JacksonRedisSerializer jacksonRedisSerializer = new JacksonRedisSerializer<>(Object.class);
        // value 值的序列化采用 fastJsonRedisSerializer
        template.setValueSerializer(jacksonRedisSerializer);
        template.setHashValueSerializer(jacksonRedisSerializer);
        // key 的序列化采用 StringRedisSerializer
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    //緩存管理器
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder
                .fromConnectionFactory(redisConnectionFactory);
        return builder.build();
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

    @Bean
    public KeyGenerator wiselyKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            Arrays.stream(params).map(Object::toString).forEach(sb::append);
            return sb.toString();
        };
    }

    @Bean
    public RedisTemplate<String, Serializable> limitRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}

class JacksonRedisSerializer<T> implements RedisSerializer<T> {
    private Class<T> clazz;
    private ObjectMapper mapper;

    JacksonRedisSerializer(Class<T> clazz) {
        super();
        this.clazz = clazz;
        this.mapper = new ObjectMapper();
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    }

    @Override
    public byte[] serialize(T t) throws SerializationException {
        try {
            return mapper.writeValueAsBytes(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException {
        if (bytes.length <= 0) {
            return null;
        }
        try {
            return mapper.readValue(bytes, clazz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

redisson 的一些配置(基礎(chǔ)版的配置) // 如有更好的配置可以自行調(diào)整與修改

package com.property.framework.config;

import com.property.framework.config.properties.RedisProperties;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.ArrayList;
import java.util.List;

/**
 * @className: RedissonConfig
 * @program: property-new-admin
 * @description: redisson配置
 * @author: buliu
 * @create: 2022-03-11 13:54
 */
@Configuration
public class RedissonConfig {
    @Autowired
    private RedisProperties redisProperties;

    @Profile("prod") // 指的是在prod 環(huán)境下注入bean
    @Bean
    public Redisson redisson() {
        //redisson版本是3.5杉允,集群的ip前面要加上“redis://”膳犹,不然會(huì)報(bào)錯(cuò),3.2版本可不加
        List<String> clusterNodes = new ArrayList<>();
        for (int i = 0; i < redisProperties.getCluster().getNodes().size(); i++) {
            clusterNodes.add("redis://" + redisProperties.getCluster().getNodes().get(i));
        }
        Config config = new Config();
        ClusterServersConfig clusterServersConfig = config.useClusterServers()
                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
        //設(shè)置密碼
        clusterServersConfig.setPassword(redisProperties.getPassword());
        return (Redisson) Redisson.create(config);
    }

    @Profile("dev") // 指的是在 dev 環(huán)境下注入bean
    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + redisProperties.getHost() + ":" + redisProperties.getPort()).setPassword(redisProperties.getPassword());
        //添加主從配置
        return Redisson.create(config);
    }
}

redis的操作service類 -這里我為了方便把拋出的Exception 給去掉了 如果 有需要可以百度

package com.property.common.core.redis;


import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface RedisService {

    /**
     * 獲取 redis 的詳細(xì)信息
     *
     * @return List
     */
    List<RedisInfo> getRedisInfo() throws RedisConnectException;

    /**
     * 獲取 redis key 數(shù)量
     *
     * @return Map
     */
    Map<String, Object> getKeysSize();

    /**
     * 獲取 redis 內(nèi)存信息
     *
     * @return Map
     */
    Map<String, Object> getMemoryInfo();

    /**
     * 獲取 key
     *
     * @param pattern 正則
     * @return Set
     */
    Set<String> getKeys(String pattern);

    /**
     * get命令
     *
     * @param key key
     * @return String
     */
    String get(String key);

    /**
     * set命令
     *
     * @param key   key
     * @param value value
     * @return String
     */
    String set(String key, String value);

    /**
     * set 命令
     *
     * @param key         key
     * @param value       value
     * @param milliscends 毫秒
     * @return String
     */
    String set(String key, String value, Long milliscends);


    /**
     * del命令
     *
     * @param key key
     * @return Long
     */
    Long del(String... key);

    /**
     * exists命令
     *
     * @param key key
     * @return Boolean
     */
    Boolean exists(String key);

    /**
     * pttl命令
     *
     * @param key key
     * @return Long
     */
    Long pttl(String key);

    /**
     * pexpire命令
     *
     * @param key         key
     * @param milliscends 毫秒
     * @return Long
     */
    Long pexpire(String key, Long milliscends);


    /**
     * zadd 命令
     *
     * @param key    key
     * @param score  score
     * @param member value
     */
    Long zadd(String key, Double score, String member);

    /**
     * zrangeByScore 命令
     *
     * @param key key
     * @param min min
     * @param max max
     * @return Set<String>
     */
    Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException;

    /**
     * zremrangeByScore 命令
     *
     * @param key   key
     * @param start start
     * @param end   end
     * @return Long
     */
    Long zremrangeByScore(String key, String start, String end);

    /**
     * zrem 命令
     *
     * @param key     key
     * @param members members
     * @return Long
     */
    Long zrem(String key, String... members);


    /**
     * 緩存基本的對(duì)象蜂林,Integer遥诉、String橡淑、實(shí)體類等
     *
     * @param key   緩存的鍵值
     * @param value 緩存的值
     * @return
     */
    public <T> String setCacheObject(final String key, final T value);

    /**
     * 獲得緩存的基本對(duì)象场晶。
     *
     * @param key 緩存鍵值
     * @return 緩存鍵值對(duì)應(yīng)的數(shù)據(jù)
     */
    public <T> T getCacheObject(final String key);


    void lget(String key, String value);

    /**
     * 自增1
     *
     * @param cacheKey
     * @param i
     * @return
     */
    Long incr(String cacheKey, int i);
}

dev 環(huán)境下的實(shí)現(xiàn)類

package com.property.common.core.redis;

import cn.hutool.core.util.SerializeUtil;
import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;
import com.property.common.function.JedisExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Client;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.*;

/**
 * Redis 工具類,只封裝了幾個(gè)常用的 redis 命令钠惩,
 * 可根據(jù)實(shí)際需要按類似的方式擴(kuò)展即可睁蕾。
 *
 * @author buliu
 */
@Profile("dev")
@Service("redisService")
public class RedisDevServiceImpl implements RedisService {

    @Autowired
    JedisPool jedisPool;

    private static final String separator = System.getProperty("line.separator");

    /**
     * 處理 jedis請(qǐng)求
     *
     * @param j 處理邏輯苞笨,通過(guò) lambda行為參數(shù)化
     * @return 處理結(jié)果
     */
    private <T> T excuteByJedis(JedisExecutor<Jedis, T> j) {
        try (Jedis jedis = jedisPool.getResource()) {
            return j.excute(jedis);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<RedisInfo> getRedisInfo() {
        String info = this.excuteByJedis(
                j -> {
                    Client client = j.getClient();
                    client.info();
                    return client.getBulkReply();
                }
        );
        List<RedisInfo> infoList = new ArrayList<>();
        String[] strs = Objects.requireNonNull(info).split(separator);
        RedisInfo redisInfo;
        if (strs.length > 0) {
            for (String str1 : strs) {
                redisInfo = new RedisInfo();
                String[] str = str1.split(":");
                if (str.length > 1) {
                    String key = str[0];
                    String value = str[1];
                    redisInfo.setKey(key);
                    redisInfo.setValue(value);
                    infoList.add(redisInfo);
                }
            }
        }
        return infoList;
    }

    @Override
    public Map<String, Object> getKeysSize() {
        Long dbSize = this.excuteByJedis(
                j -> {
                    Client client = j.getClient();
                    client.dbSize();
                    return client.getIntegerReply();
                }
        );
        Map<String, Object> map = new HashMap<>();
        map.put("create_time", System.currentTimeMillis());
        map.put("dbSize", dbSize);
        return map;
    }

    @Override
    public Map<String, Object> getMemoryInfo() {
        String info = this.excuteByJedis(
                j -> {
                    Client client = j.getClient();
                    client.info();
                    return client.getBulkReply();
                }
        );
        String[] strs = Objects.requireNonNull(info).split(separator);
        Map<String, Object> map = null;
        for (String s : strs) {
            String[] detail = s.split(":");
            if ("used_memory".equals(detail[0])) {
                map = new HashMap<>();
                map.put("used_memory", detail[1].substring(0, detail[1].length() - 1));
                map.put("create_time", System.currentTimeMillis());
                break;
            }
        }
        return map;
    }

    @Override
    public Set<String> getKeys(String pattern) {
        return this.excuteByJedis(j -> j.keys(pattern));
    }

    @Override
    public String get(String key) {
        return this.excuteByJedis(j -> j.get(key.toLowerCase()));
    }

    @Override
    public String set(String key, String value) {
        return this.excuteByJedis(j -> j.set(key.toLowerCase(), value));
    }

    @Override
    public String set(String key, String value, Long milliscends) {
        String result = this.set(key.toLowerCase(), value);
        this.pexpire(key, milliscends);
        return result;
    }


    @Override
    public Long del(String... key) {
        return this.excuteByJedis(j -> j.del(key));
    }

    @Override
    public Boolean exists(String key) {
        return this.excuteByJedis(j -> j.exists(key));
    }

    @Override
    public Long pttl(String key) {
        return this.excuteByJedis(j -> j.pttl(key));
    }

    @Override
    public Long pexpire(String key, Long milliseconds) {
        return this.excuteByJedis(j -> j.pexpire(key, milliseconds));
    }

    @Override
    public Long zadd(String key, Double score, String member) {
        return this.excuteByJedis(j -> j.zadd(key, score, member));
    }

    @Override
    public Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException {
        return this.excuteByJedis(j -> j.zrangeByScore(key, min, max));
    }

    @Override
    public Long zremrangeByScore(String key, String start, String end) {
        return this.excuteByJedis(j -> j.zremrangeByScore(key, start, end));
    }

    @Override
    public Long zrem(String key, String... members) {
        return this.excuteByJedis(j -> j.zrem(key, members));
    }

    /**
     * 緩存基本的對(duì)象,Integer子眶、String猫缭、實(shí)體類等
     *
     * @param key   緩存的鍵值
     * @param value 緩存的值
     * @return
     */
    @Override
    public <T> String setCacheObject(String key, T value) {
        return this.excuteByJedis(j -> j.set(key.getBytes(), SerializeUtil.serialize(value)));
    }


    /**
     * 獲得緩存的基本對(duì)象。
     *
     * @param key 緩存鍵值
     * @return 緩存鍵值對(duì)應(yīng)的數(shù)據(jù)
     */
    @Override
    public <T> T getCacheObject(String key) {
        return (T) this.excuteByJedis(j -> j.get(key.getBytes()));
    }

    @Override
    public void lget(String key, String value) {
        // return this.excuteByJedis(j -> j.(key, members));
    }

    /**
     * 自增1
     *
     * @param cacheKey
     * @param i
     * @return
     */
    @Override
    public Long incr(String cacheKey, int i) {
        return this.excuteByJedis(j -> j.incrBy(cacheKey, i));
    }


}

prod 實(shí)現(xiàn)類

package com.property.common.core.redis;

import cn.hutool.core.util.SerializeUtil;
import com.property.common.core.domain.RedisInfo;
import com.property.common.exception.RedisConnectException;
import com.property.common.function.JedisExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import java.util.*;

/**
 * Redis 工具類壹店,只封裝了幾個(gè)常用的 redis 命令,
 * 可根據(jù)實(shí)際需要按類似的方式擴(kuò)展即可芝加。
 *
 * @author buliu
 */
@Profile("prod")
@Service("redisService")
public class RedisServiceImpl implements RedisService {

    @Autowired
    JedisCluster jedisPool;

    private static final String separator = System.getProperty("line.separator");

    /**
     * 處理 jedis請(qǐng)求
     *
     * @param j 處理邏輯硅卢,通過(guò) lambda行為參數(shù)化
     * @return 處理結(jié)果
     */
    private <T> T excuteByJedis(JedisExecutor<JedisCluster, T> j) {
        try  {
            return j.excute(jedisPool);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public List<RedisInfo> getRedisInfo() {
        // String info = this.excuteByJedis(
        //         j -> {
        //             Client client = j.getClient();
        //             client.info();
        //             return client.getBulkReply();
        //         }
        // );
        // String[] strs = Objects.requireNonNull(info).split(separator);
        // RedisInfo redisInfo;
        // if (strs.length > 0) {
        //     for (String str1 : strs) {
        //         redisInfo = new RedisInfo();
        //         String[] str = str1.split(":");
        //         if (str.length > 1) {
        //             String key = str[0];
        //             String value = str[1];
        //             redisInfo.setKey(key);
        //             redisInfo.setValue(value);
        //             infoList.add(redisInfo);
        //         }
        //     }
        // }
        return new ArrayList<>();
    }

    @Override
    public Map<String, Object> getKeysSize() {
        // Long dbSize = this.excuteByJedis(
        //         j -> {
        //             Client client = j.getClient();
        //             client.dbSize();
        //             return client.getIntegerReply();
        //         }
        // );
        Map<String, Object> map = new HashMap<>();
        // map.put("create_time", System.currentTimeMillis());
        // map.put("dbSize", dbSize);
        return map;
    }

    @Override
    public Map<String, Object> getMemoryInfo() {
        // String info = this.excuteByJedis(
        //         j -> {
        //             Client client = j.getClient();
        //             client.info();
        //             return client.getBulkReply();
        //         }
        // );
        // String[] strs = Objects.requireNonNull(info).split(separator);
        // for (String s : strs) {
        //     String[] detail = s.split(":");
        //     if ("used_memory".equals(detail[0])) {
        //         map = new HashMap<>();
        //         map.put("used_memory", detail[1].substring(0, detail[1].length() - 1));
        //         map.put("create_time", System.currentTimeMillis());
        //         break;
        //     }
        // }
        return null;
    }

    @Override
    public Set<String> getKeys(String pattern) {
        return this.excuteByJedis(j -> j.keys(pattern));
    }

    @Override
    public String get(String key) {
        return this.excuteByJedis(j -> j.get(key.toLowerCase()));
    }

    @Override
    public String set(String key, String value) {
        return this.excuteByJedis(j -> j.set(key.toLowerCase(), value));
    }

    @Override
    public String set(String key, String value, Long milliscends) {
        String result = this.set(key.toLowerCase(), value);
        this.pexpire(key, milliscends);
        return result;
    }


    @Override
    public Long del(String... key) {
        return this.excuteByJedis(j -> j.del(key));
    }

    @Override
    public Boolean exists(String key) {
        return this.excuteByJedis(j -> j.exists(key));
    }

    @Override
    public Long pttl(String key) {
        return this.excuteByJedis(j -> j.pttl(key));
    }

    @Override
    public Long pexpire(String key, Long milliseconds) {
        return this.excuteByJedis(j -> j.pexpire(key, milliseconds));
    }

    @Override
    public Long zadd(String key, Double score, String member) {
        return this.excuteByJedis(j -> j.zadd(key, score, member));
    }

    @Override
    public Set<String> zrangeByScore(String key, String min, String max) throws RedisConnectException {
        return this.excuteByJedis(j -> j.zrangeByScore(key, min, max));
    }

    @Override
    public Long zremrangeByScore(String key, String start, String end) {
        return this.excuteByJedis(j -> j.zremrangeByScore(key, start, end));
    }

    @Override
    public Long zrem(String key, String... members) {
        return this.excuteByJedis(j -> j.zrem(key, members));
    }

    /**
     * 緩存基本的對(duì)象,Integer藏杖、String将塑、實(shí)體類等
     *
     * @param key   緩存的鍵值
     * @param value 緩存的值
     * @return
     */
    @Override
    public <T> String setCacheObject(String key, T value) {
        return this.excuteByJedis(j -> j.set(key.getBytes(), SerializeUtil.serialize(value)));
    }


    /**
     * 獲得緩存的基本對(duì)象。
     *
     * @param key 緩存鍵值
     * @return 緩存鍵值對(duì)應(yīng)的數(shù)據(jù)
     */
    @Override
    public <T> T getCacheObject(String key) {
        return (T) this.excuteByJedis(j -> j.get(key.getBytes()));
    }

    @Override
    public void lget(String key, String value) {
        // return this.excuteByJedis(j -> j.(key, members));
    }

    /**
     * 自增1
     *
     * @param cacheKey
     * @param i
     * @return
     */
    @Override
    public Long incr(String cacheKey, int i) {
        return this.excuteByJedis(j -> j.incrBy(cacheKey, i));
    }


}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蝌麸,一起剝皮案震驚了整個(gè)濱河市点寥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌来吩,老刑警劉巖敢辩,帶你破解...
    沈念sama閱讀 216,692評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異弟疆,居然都是意外死亡戚长,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門怠苔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)同廉,“玉大人,你說(shuō)我怎么就攤上這事∑刃ぃ” “怎么了锅劝?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,995評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蟆湖。 經(jīng)常有香客問(wèn)我故爵,道長(zhǎng),這世上最難降的妖魔是什么帐姻? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,223評(píng)論 1 292
  • 正文 為了忘掉前任稠集,我火速辦了婚禮,結(jié)果婚禮上饥瓷,老公的妹妹穿的比我還像新娘剥纷。我一直安慰自己,他們只是感情好呢铆,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布晦鞋。 她就那樣靜靜地躺著,像睡著了一般棺克。 火紅的嫁衣襯著肌膚如雪悠垛。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,208評(píng)論 1 299
  • 那天娜谊,我揣著相機(jī)與錄音确买,去河邊找鬼。 笑死纱皆,一個(gè)胖子當(dāng)著我的面吹牛湾趾,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播派草,決...
    沈念sama閱讀 40,091評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼搀缠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了近迁?” 一聲冷哼從身側(cè)響起艺普,我...
    開(kāi)封第一講書(shū)人閱讀 38,929評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鉴竭,沒(méi)想到半個(gè)月后歧譬,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評(píng)論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搏存,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評(píng)論 2 333
  • 正文 我和宋清朗相戀三年缴罗,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片祭埂。...
    茶點(diǎn)故事閱讀 39,739評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡面氓,死狀恐怖兵钮,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情舌界,我是刑警寧澤掘譬,帶...
    沈念sama閱讀 35,437評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站呻拌,受9級(jí)特大地震影響葱轩,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜藐握,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評(píng)論 3 326
  • 文/蒙蒙 一靴拱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧猾普,春花似錦袜炕、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,677評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至溜在,卻和暖如春陌知,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背掖肋。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,833評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工仆葡, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人志笼。 一個(gè)月前我還...
    沈念sama閱讀 47,760評(píng)論 2 369
  • 正文 我出身青樓浙芙,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親籽腕。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評(píng)論 2 354