redis集群使用Java工具类(Java jedis集群工具类)

package com.xiaomi.weather.vote.webservices.util.redisCache;

import com.google.common.base.Strings;
import org.apache.log4j.Logger;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.InputStream;
import java.util.*;

/**
 * Created by mi on 16-12-22.
 */
public class RedisClusterClient {
    private static final Logger logger = Logger.getLogger(RedisClusterClient.class);
    private static String isOn = "true"; // 是否启用缓存
    private static JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 配置信息
    public JedisCluster jedisCluster = null;
    Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

    public static RedisClusterClient redisClusterClient = new RedisClusterClient();

    public RedisClusterClient() {
        init();
    }

    public static RedisClusterClient getInsance() {
        if (redisClusterClient != null) {
            return redisClusterClient;
        } else {
            redisClusterClient = new RedisClusterClient();
            return redisClusterClient;
        }
    }

    public boolean init() {
        try {
            // 读取配置文件
            InputStream path = RedisClusterClient.class.getClassLoader().getResourceAsStream("cache.properties");
            Properties pros = new Properties();
            pros.load(path);
            this.isOn = pros.getProperty("redis.onoff", "true");// 默认开启缓存
            if (this.isOn.equals("false")) {// 未开启缓存
                return false;
            }

            //
            String servers = pros.getProperty("redisMultiCluster.clusters", null);
            if (Strings.isNullOrEmpty(servers)) {
                logger.error("RedisJavaClient.servers 配置错误; in file:cache.properties");
                this.isOn = "false";
                return false;
            }
            String[] hostAndPorts = servers.split("\|");
            for (int i = 0; i < hostAndPorts.length; i++) {
                String hostAndPort = hostAndPorts[i];
                if (!hostAndPort.contains(":")) {
                    return false;
                }
                jedisClusterNodes.add(new HostAndPort(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1])));
            }
            try {
                jedisPoolConfig.setMaxTotal(Integer.parseInt(pros.getProperty("redisMultiCluster.maxTotal", "8")));
                jedisPoolConfig.setMaxIdle(Integer.parseInt(pros.getProperty("redisMultiCluster.maxIdle", "8")));
                jedisPoolConfig.setMinIdle(Integer.parseInt(pros.getProperty("redisMultiCluster.minIdle", "0")));
                jedisPoolConfig
                        .setBlockWhenExhausted(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.blockWhenExhausted", "true")));
                jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(pros.getProperty("redisMultiCluster.maxWaitMillis", "true")));
                jedisPoolConfig.setTestOnBorrow(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnBorrow", "true")));
                jedisPoolConfig.setTestOnCreate(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnCreate", "true")));
                jedisPoolConfig.setTestOnReturn(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnReturn", "true")));
                jedisPoolConfig.setTestWhileIdle(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testWhileIdle", "false")));

            } catch (Exception e) {
                logger.error("{未知异常}", e);
            }
            jedisCluster = new JedisCluster(jedisClusterNodes, jedisPoolConfig);
            logger.info("缓存初始化成功");
            path.close();
            return true;
        } catch (Exception e) {
            logger.error("{缓存初始化错误}", e);
            this.isOn = "false";
        }
        return false;
    }

    public boolean setnx(String key, String value, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            Long setSuccess = jedisCluster.setnx(key, value);
            if (setSuccess == 1) {
                if (1 == jedisCluster.expire(key, expireTime)) {
                    return true;
                } else {
                    this.deleteKey(key);
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return false;
    }

    public boolean set(String key, String value, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.setex(key, expireTime, value);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return false;
    }

    public String get(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return "";
        }
        try {
            return jedisCluster.get(key);
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return null;
    }

    public <T> boolean setList(String key, List<T> list, Class<T> tClass, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            if (this.setnx(key + "Lock", "true", expireTime)) {
                this.deleteKey(key);
                for (T t : list) {
                    jedisCluster.rpush(key, SerializationDefine.Object2String(t));
                }
                if (1 == jedisCluster.expire(key, expireTime)) {
                    return true;
                } else {
                    this.deleteKey(key);
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return false;
    }

    public <T> List<T> getList(String key, Class<T> tClass) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return null;
        }
        try {
            List<T> tList = new ArrayList<T>();
            List<String> strList = jedisCluster.lrange(key, 0L, -1L);
            for (String str : strList) {
                tList.add(SerializationDefine.String2Object(str, tClass));
            }

            return tList.size() == 0 ? null : tList;
        } catch (Exception e) {
            logger.error("{}", e);
            return null;
        }
    }

    public <T> boolean setMap(String key, Map<String, T> map, Class<T> tClass, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        Map<String, String> catchMap = new HashMap<String, String>();
        if (this.setnx(key + "Lock", "true", expireTime)) {
            this.deleteKey(key);
            for (Map.Entry<String, T> entry : map.entrySet()) {
                catchMap.put(entry.getKey(), SerializationDefine.Object2String(entry.getValue()));
            }
            jedisCluster.hmset(key, catchMap);
            if (1 == jedisCluster.expire(key, expireTime)) {
                return true;
            } else {
                this.deleteKey(key);
                return false;
            }

        }
        return true;
    }

    public <T> Map<String, T> getMap(String key, Class<T> tClass) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return null;
        }
        try {
            Map<String, String> catchMap = jedisCluster.hgetAll(key);
            Map<String, T> retMap = new HashMap<String, T>();
            for (Map.Entry<String, String> entry : catchMap.entrySet()) {
                retMap.put(entry.getKey(), SerializationDefine.String2Object(entry.getValue(), tClass));
            }
            return retMap;
        } catch (Exception e) {
            logger.error("{}", e);
            return null;
        }

    }

    public boolean deleteKey(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            if (1 == jedisCluster.del(key)) {
                return true;
            } else {
                throw new Exception("redis异常,删除key失败:method = deleteKey; key = " + key);
            }
        } catch (Exception e) {
            logger.error("{严重异常}", e);
        }

        return false;
    }

    /**
     * 加1操作
     *
     * @param key
     * @return
     */
    public boolean incr(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.incr(key);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
            return false;
        }
    }

    /**
     * 减1操作
     *
     * @param key
     * @return
     */
    public boolean decr(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.decr(key);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
            return false;
        }
    }

    /**
     * 判断是否已缓存
     *
     * @param key
     * @return
     */
    public boolean isExist(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            return jedisCluster.exists(key);
        } catch (Exception e) {
            return false;
        }
    }
}
RedisClusterClient

说明,当redis连接不够用时,此setnx可能存在一些bug,即超时时间可能设置不成功!

所以当存在连接可能不够用的情况时,尽量不要使用setnx。

配置文件:

redis.onoff=true
redis.defaultExpireInSecond=900
redis.namespace=${cache_namespace}
redis.prefix=api-v3
RedisJavaClient.onoff=true
RedisJavaClient.servers=${redis_servers}
##==========================================================
redisMultiCluster.defaultExpireInSecond=900
redisMultiCluster.prefix=location-v3
redisMultiCluster.namespace=${cache_namespace}
redisMultiCluster.onoff=true
redisMultiCluster.clusters=${redis_clusters}
redisMultiCluster.route=route1:0|route2:1
redisMultiCluster.rule=com.xiaomi.weather.commons.cache.SubKeyRule
redisMultiCluster.startIndex=0
redisMultiCluster.endIndex=5

##-------------连接池配置------------------
#最大连接数,最大资源空闲数目,最小资源空闲数目
redisMultiCluster.maxTotal=30
redisMultiCluster.maxIdle=30
redisMultiCluster.minIdle=10
#连接耗尽时,是否等待;等待时长(ms)
redisMultiCluster.blockWhenExhausted=true
redisMultiCluster.maxWaitMillis=500

#输出连接时是否检测空闲超时,回收连接检测,创建连接检测,输出连接检测,
redisMultiCluster.testWhileIdle=false
redisMultiCluster.testOnReturn=true
redisMultiCluster.testOnCreate=true
redisMultiCluster.testOnBorrow=true

#(暂时不可用)资源耗尽时的处理措施0 - 抛异常,1 - 阻塞等待可用资源,2-强制创建新连接
redisMultiCluster.whenExhaustedAction=1
View Code

序列化类:

package com.xiaomi.weather.vote.webservices.util.redisCache;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;

/**
 * Created by mi on 16-12-22.
 */
public class SerializationDefine {
    public static String Object2String(Object obj) {
        Preconditions.checkArgument(obj != null, "序列化对象为null");
        return JSONObject.toJSONString(obj);
    }

    public static <T> T String2Object(String str, Class<T> tClass) {
        return JSONObject.parseObject(str, tClass);
    }
}
View Code

工具类第二版,修复删除key,而key不存在时报异常的bug;

package com.xiaomi.weather.vote.webservices.util.redisCache;

import com.google.common.base.Strings;
import org.apache.log4j.Logger;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.InputStream;
import java.util.*;

/**
 * Created by mi on 16-12-22.
 */
public class RedisClusterClient {
    private static final Logger logger = Logger.getLogger(RedisClusterClient.class);
    private static String isOn = "true"; // 是否启用缓存
    private static JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // 配置信息
    public JedisCluster jedisCluster = null;
    Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();

    public static RedisClusterClient redisClusterClient = new RedisClusterClient();

    public RedisClusterClient() {
        init();
    }

    public static RedisClusterClient getInsance() {
        if (redisClusterClient != null) {
            return redisClusterClient;
        } else {
            redisClusterClient = new RedisClusterClient();
            return redisClusterClient;
        }
    }

    public boolean init() {
        try {
            // 读取配置文件
            InputStream path = RedisClusterClient.class.getClassLoader().getResourceAsStream("cache.properties");
            Properties pros = new Properties();
            pros.load(path);
            this.isOn = pros.getProperty("redis.onoff", "true");// 默认开启缓存
            if (this.isOn.equals("false")) {// 未开启缓存
                return false;
            }

            //
            String servers = pros.getProperty("redisMultiCluster.clusters", null);
            if (Strings.isNullOrEmpty(servers)) {
                logger.error("RedisJavaClient.servers 配置错误; in file:cache.properties");
                this.isOn = "false";
                return false;
            }
            String[] hostAndPorts = servers.split("\|");
            for (int i = 0; i < hostAndPorts.length; i++) {
                String hostAndPort = hostAndPorts[i];
                if (!hostAndPort.contains(":")) {
                    return false;
                }
                jedisClusterNodes.add(new HostAndPort(hostAndPort.split(":")[0], Integer.parseInt(hostAndPort.split(":")[1])));
            }
            try {
                jedisPoolConfig.setMaxTotal(Integer.parseInt(pros.getProperty("redisMultiCluster.maxTotal", "8")));
                jedisPoolConfig.setMaxIdle(Integer.parseInt(pros.getProperty("redisMultiCluster.maxIdle", "8")));
                jedisPoolConfig.setMinIdle(Integer.parseInt(pros.getProperty("redisMultiCluster.minIdle", "0")));
                jedisPoolConfig
                        .setBlockWhenExhausted(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.blockWhenExhausted", "true")));
                jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(pros.getProperty("redisMultiCluster.maxWaitMillis", "true")));
                jedisPoolConfig.setTestOnBorrow(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnBorrow", "true")));
                jedisPoolConfig.setTestOnCreate(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnCreate", "true")));
                jedisPoolConfig.setTestOnReturn(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testOnReturn", "true")));
                jedisPoolConfig.setTestWhileIdle(Boolean.parseBoolean(pros.getProperty("redisMultiCluster.testWhileIdle", "false")));

            } catch (Exception e) {
                logger.error("{未知异常}", e);
            }
            jedisCluster = new JedisCluster(jedisClusterNodes, jedisPoolConfig);
            logger.info("缓存初始化成功");
            path.close();
            return true;
        } catch (Exception e) {
            logger.error("{缓存初始化错误}", e);
            this.isOn = "false";
        }
        return false;
    }

    public boolean setnx(String key, String value, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            Long setSuccess = jedisCluster.setnx(key, value);
            if (setSuccess == 1) {//写入成功
                if (1 == jedisCluster.expire(key, expireTime)) {
                    return true;
                } else {
                    this.deleteKey(key);
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            try {
                this.deleteKey(key);
            } catch (Exception ex) {
                logger.error("删除key异常:key = " + key);
            }
            logger.error("{}", e);
        }
        return false;
    }

    public boolean set(String key, String value, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.setex(key, expireTime, value);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return false;
    }

    public String get(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return "";
        }
        try {
            return jedisCluster.get(key);
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return null;
    }

    public <T> boolean setList(String key, List<T> list, Class<T> tClass, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            if (this.setnx(key + "Lock", "true", expireTime)) {
                this.deleteKey(key);
                for (T t : list) {
                    jedisCluster.rpush(key, SerializationDefine.Object2String(t));
                }
                if (1 == jedisCluster.expire(key, expireTime)) {
                    return true;
                } else {
                    this.deleteKey(key);
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
        }
        return false;
    }

    public <T> List<T> getList(String key, Class<T> tClass) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return null;
        }
        try {
            List<T> tList = new ArrayList<T>();
            List<String> strList = jedisCluster.lrange(key, 0L, -1L);
            for (String str : strList) {
                tList.add(SerializationDefine.String2Object(str, tClass));
            }

            return tList.size() == 0 ? null : tList;
        } catch (Exception e) {
            logger.error("{}", e);
            return null;
        }
    }

    public <T> boolean setMap(String key, Map<String, T> map, Class<T> tClass, int expireTime) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        Map<String, String> catchMap = new HashMap<String, String>();
        try {
            if (this.setnx(key + "Lock", "true", expireTime)) {
                this.deleteKey(key);
                for (Map.Entry<String, T> entry : map.entrySet()) {
                    catchMap.put(entry.getKey(), SerializationDefine.Object2String(entry.getValue()));
                }
                jedisCluster.hmset(key, catchMap);
                if (1 == jedisCluster.expire(key, expireTime)) {
                    return true;
                } else {
                    this.deleteKey(key);
                    return false;
                }

            }
        } catch (Exception e) {
            logger.error("{严重异常}", e);
        }
        return true;
    }

    public <T> Map<String, T> getMap(String key, Class<T> tClass) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return null;
        }
        try {
            Map<String, String> catchMap = jedisCluster.hgetAll(key);
            Map<String, T> retMap = new HashMap<String, T>();
            for (Map.Entry<String, String> entry : catchMap.entrySet()) {
                retMap.put(entry.getKey(), SerializationDefine.String2Object(entry.getValue(), tClass));
            }
            return retMap;
        } catch (Exception e) {
            logger.error("{}", e);
            return null;
        }

    }

    public boolean deleteKey(String key) throws Exception {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            if (!this.isExist(key)) {//key不存在
                return true;
            }
            if (1 == jedisCluster.del(key)) {
                return true;
            } else {
                throw new Exception("redis异常,删除key失败:method = deleteKey; key = " + key);
            }
        } catch (Exception e) {
            logger.error("{严重异常}", e);
            throw new Exception("redis异常,删除key失败:method = deleteKey; key = " + key);
        }
    }

    /**
     * 加1操作
     *
     * @param key
     * @return
     */
    public boolean incr(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.incr(key);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
            return false;
        }
    }

    /**
     * 减1操作
     *
     * @param key
     * @return
     */
    public boolean decr(String key) {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            jedisCluster.decr(key);
            return true;
        } catch (Exception e) {
            logger.error("{}", e);
            return false;
        }
    }

    /**
     * 判断是否已缓存
     *
     * @param key
     * @return
     */
    public boolean isExist(String key) throws Exception {
        if (!isOn.equals("true")) {
            logger.info("缓存未开启");
            return false;
        }
        try {
            return jedisCluster.exists(key);
        } catch (Exception e) {
            logger.error("{严重异常}判断key是否存在发生异常... ... key = " + key, e);
            throw new Exception("{严重异常}判断key是否存在发生异常... ...key = " + key);
        }
    }

    public static void main(String[] arges) {
        try {
            System.out.println(RedisClusterClient.getInsance().jedisCluster.del("panpanpan"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
View Code
原文地址:https://www.cnblogs.com/tengpan-cn/p/6478466.html