博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
redis集群使用Java工具类(Java jedis集群工具类)
阅读量:5277 次
发布时间:2019-06-14

本文共 19206 字,大约阅读时间需要 64 分钟。

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
jedisClusterNodes = new HashSet
(); 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
boolean setList(String key, List
list, Class
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
List
getList(String key, Class
tClass) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return null; } try { List
tList = new ArrayList
(); List
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
boolean setMap(String key, Map
map, Class
tClass, int expireTime) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return false; } Map
catchMap = new HashMap
(); if (this.setnx(key + "Lock", "true", expireTime)) { this.deleteKey(key); for (Map.Entry
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
Map
getMap(String key, Class
tClass) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return null; } try { Map
catchMap = jedisCluster.hgetAll(key); Map
retMap = new HashMap
(); for (Map.Entry
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=trueredis.defaultExpireInSecond=900redis.namespace=${cache_namespace}redis.prefix=api-v3RedisJavaClient.onoff=trueRedisJavaClient.servers=${redis_servers}##==========================================================redisMultiCluster.defaultExpireInSecond=900redisMultiCluster.prefix=location-v3redisMultiCluster.namespace=${cache_namespace}redisMultiCluster.onoff=trueredisMultiCluster.clusters=${redis_clusters}redisMultiCluster.route=route1:0|route2:1redisMultiCluster.rule=com.xiaomi.weather.commons.cache.SubKeyRuleredisMultiCluster.startIndex=0redisMultiCluster.endIndex=5##-------------连接池配置------------------#最大连接数,最大资源空闲数目,最小资源空闲数目redisMultiCluster.maxTotal=30redisMultiCluster.maxIdle=30redisMultiCluster.minIdle=10#连接耗尽时,是否等待;等待时长(ms)redisMultiCluster.blockWhenExhausted=trueredisMultiCluster.maxWaitMillis=500#输出连接时是否检测空闲超时,回收连接检测,创建连接检测,输出连接检测,redisMultiCluster.testWhileIdle=falseredisMultiCluster.testOnReturn=trueredisMultiCluster.testOnCreate=trueredisMultiCluster.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 String2Object(String str, Class
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
jedisClusterNodes = new HashSet
(); 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
boolean setList(String key, List
list, Class
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
List
getList(String key, Class
tClass) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return null; } try { List
tList = new ArrayList
(); List
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
boolean setMap(String key, Map
map, Class
tClass, int expireTime) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return false; } Map
catchMap = new HashMap
(); try { if (this.setnx(key + "Lock", "true", expireTime)) { this.deleteKey(key); for (Map.Entry
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
Map
getMap(String key, Class
tClass) { if (!isOn.equals("true")) { logger.info("缓存未开启"); return null; } try { Map
catchMap = jedisCluster.hgetAll(key); Map
retMap = new HashMap
(); for (Map.Entry
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

你可能感兴趣的文章
java实用类
查看>>
smarty模板自定义变量
查看>>
研究称90%的癌症由非健康生活习惯导致
查看>>
命令行启动Win7系统操作部分功能
查看>>
排序sort (一)
查看>>
Parrot虚拟机
查看>>
Teamcenter10 step-by-step installation in Linux env-Oracle Server Patch
查看>>
Struts2学习(三)
查看>>
Callable和Runnable和FutureTask
查看>>
GitHub 多人协作开发 三种方式:
查看>>
文本域添加编辑器
查看>>
Yum安装MySQL以及相关目录路径和修改目录
查看>>
java获取hostIp和hostName
查看>>
关于web服务器和数据库的各种说法(搜集到的)
查看>>
《TCP/IP 详解 卷一》读书笔记 -----第四章 ARP
查看>>
C# Stream 和 byte[] 之间的转换
查看>>
OMG: daily scrum nine
查看>>
redis与spring结合错误情况
查看>>
第六章 字节码执行方式--解释执行和JIT
查看>>
实现绘制图形的ToolBar
查看>>