关于jedis2.4以上版本的连接池配置,及工具类

jedis.propertise 注意以前版本的maxAcitve和maxWait有所改变,JVM根据系统环境变量ServerType中的值 取不同的配置,实现多环境(测试环境、生产环境)集成。

redis.pool.maxTotal=redis.pool.maxActive.${ServerType}
redis.pool.maxIdle=redis.pool.maxIdle.${ServerType}
redis.pool.maxWaitMillis=redis.pool.maxWait.${ServerType}
redis.pool.minIdle=redis.pool.minIdle.${ServerType}
redis.host=redis.host.${ServerType}
redis.port=redis.port.${ServerType}
redis.password=redis.password.${ServerType}
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.timeout=1000
#C
#REL
redis.pool.maxActive.REL=200
redis.pool.maxIdle.REL=50
redis.pool.minIdle.REL=10
redis.pool.maxWait.REL=300
redis.host.REL=192.168.0.201
redis.port.REL=8989
redis.password.REL=beidou123#VRF
redis.pool.maxActive.VRF=200
redis.pool.maxIdle.VRF=50
redis.pool.minIdle.VRF=10
redis.pool.maxWait.VRF=300
redis.host.VRF=192.168.0.201
redis.port.VRF=8989
redis.password.VRF=beidou123

config的包装便于后面xml的注入

/*** redis连接池配置文件包装类** @author daxiong*         date: 2017/03/07 11:51*/
public class GenericObjectPoolConfigWrapper extends GenericObjectPoolConfig {public GenericObjectPoolConfig getConfig() {return this;}}

 

spring配置

<!--redis连接池配置--><context:property-placeholder location="classpath:jedis.properties" ignore-unresolvable="true"/><bean id="jedisPoolConfig" class="smm.mvc.CacheDb.GenericObjectPoolConfigWrapper"><!--最大连接数--><property name="maxTotal" value="${${redis.pool.maxTotal}}" /><!--最大空闲连接数--><property name="maxIdle" value="${${redis.pool.maxIdle}}" /><!--初始化连接数--><property name="minIdle" value="${${redis.pool.minIdle}}"/><!--最大等待时间--><property name="maxWaitMillis" value="${${redis.pool.maxWaitMillis}}" /><!--对拿到的connection进行validateObject校验--><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /><!--在进行returnObject对返回的connection进行validateObject校验--><property name="testOnReturn" value="${redis.pool.testOnReturn}" /><!--定时对线程池中空闲的链接进行validateObject校验--><property name="testWhileIdle" value="true" /></bean><bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="destroy"><constructor-arg index="0"><bean factory-bean="jedisPoolConfig" factory-method="getConfig"/></constructor-arg><constructor-arg index="1" value="${${redis.host}}"/><constructor-arg index="2" value="${${redis.port}}"/><!--timeout--><constructor-arg index="3" value="${redis.timeout}"/><constructor-arg index="4" value="${${redis.password}}"/></bean>

 

jedis工具类

ublic abstract class JCacheTools {public abstract int getDBIndex();/*** 默认日志打印logger_default*/public static Logger logger_default = Logger.getLogger("logger_jCache_default");/*** 失败日志logger,用于定期del指定的key*/public static Logger logger_failure = Logger.getLogger("logger_jCache_failure");@Resourceprotected JedisPool jedisPool;protected Jedis getJedis() throws JedisException {Jedis jedis = null;try {jedis = jedisPool.getResource();} catch (JedisException e) {LogContext.instance().warn(logger_failure, "failed:jedisPool getResource.", e);if(jedis!=null){jedisPool.returnBrokenResource(jedis);}throw e;}return jedis;}protected void release(Jedis jedis, boolean isBroken) {if (jedis != null) {if (isBroken) {jedisPool.returnBrokenResource(jedis);} else {jedisPool.returnResource(jedis);}}}protected String addStringToJedis(String key, String value, int cacheSeconds, String methodName) {Jedis jedis = null;boolean isBroken = false;String lastVal = null;try {jedis = this.getJedis();jedis.select(getDBIndex());lastVal = jedis.getSet(key, value);if(cacheSeconds!=0){jedis.expire(key,cacheSeconds);}LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);} finally {release(jedis, isBroken);}return lastVal;}protected void addStringToJedis(Map<String,String> batchData, int cacheSeconds, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());Pipeline pipeline = jedis.pipelined();for(Map.Entry<String,String> element:batchData.entrySet()){if(cacheSeconds!=0){pipeline.setex(element.getKey(),cacheSeconds,element.getValue());}else{pipeline.set(element.getKey(),element.getValue());}}pipeline.sync();LogContext.instance().debug(logger_default, "succeed:" + methodName,batchData.size());} catch (Exception e) {isBroken = true;e.printStackTrace();} finally {release(jedis, isBroken);}}protected void addListToJedis(String key, List<String> list, int cacheSeconds, String methodName) {if (list != null && list.size() > 0) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());if (jedis.exists(key)) {jedis.del(key);}for (String aList : list) {jedis.rpush(key, aList);}if(cacheSeconds!=0){jedis.expire(key, cacheSeconds);}LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list.size());} catch (JedisException e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list.size(), e);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list.size(), e);} finally {release(jedis, isBroken);}}}protected void addToSetJedis(String key, String[] value, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.sadd(key,value);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);} finally {release(jedis, isBroken);}}protected void removeSetJedis(String key,String value, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.srem(key,value);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);} finally {release(jedis, isBroken);}}protected void pushDataToListJedis(String key, String data, int cacheSeconds, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.rpush(key, data);if(cacheSeconds!=0){jedis.expire(key,cacheSeconds);}LogContext.instance().debug(logger_default, "succeed:" + methodName, key, data);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, data, e);} finally {release(jedis, isBroken);}}protected void pushDataToListJedis(String key,List<String> batchData, int cacheSeconds, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.del(key);jedis.lpush(key,batchData.toArray(new String[batchData.size()]));if(cacheSeconds!=0)jedis.expire(key,cacheSeconds);LogContext.instance().debug(logger_default, "succeed:" + methodName,batchData!=null?batchData.size():0);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, batchData!=null?batchData.size():0, e);} finally {release(jedis, isBroken);}}/*** 删除list中的元素* @param key* @param values* @param methodName*/protected void deleteDataFromListJedis(String key,List<String> values, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());Pipeline pipeline = jedis.pipelined();if(values!=null && !values.isEmpty()){for (String val:values){pipeline.lrem(key,0,val);}}pipeline.sync();LogContext.instance().debug(logger_default, "succeed:" + methodName,values!=null?values.size():0);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, values!=null?values.size():0, e);} finally {release(jedis, isBroken);}}protected void addHashMapToJedis(String key, Map<String, String> map, int cacheSeconds, boolean isModified, String methodName) {boolean isBroken = false;Jedis jedis = null;if (map != null && map.size() > 0) {try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.hmset(key, map);if (cacheSeconds >= 0)jedis.expire(key, cacheSeconds);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, map.size());} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, map.size(), e);} finally {release(jedis, isBroken);}}}protected void addHashMapToJedis(String key, String field, String value, int cacheSeconds, String methodName) {boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();if (jedis != null) {jedis.select(getDBIndex());jedis.hset(key, field, value);jedis.expire(key, cacheSeconds);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, field, value);}} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, field, value, e);}finally {release(jedis, isBroken);}}protected void updateHashMapToJedis(String key, String incrementField, long incrementValue, String dateField, String dateValue, String methodName) {boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();jedis.select(getDBIndex());jedis.hincrBy(key, incrementField, incrementValue);jedis.hset(key, dateField, dateValue);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, incrementField, incrementValue);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, incrementField, incrementValue, e);}finally {release(jedis, isBroken);}}public String getStringFromJedis(String key, String methodName) {String value = null;Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());if (jedis.exists(key)) {value = jedis.get(key);value = StringUtils.isNotBlank(value) && !"nil".equalsIgnoreCase(value)?value:null;LogContext.instance().debug(logger_default, "succeed:" + methodName, key, value);}} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, value, e);} finally {release(jedis, isBroken);}return value;}public List<String> getStringFromJedis(String[] keys, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());return jedis.mget(keys);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, Arrays.toString(keys), e);} finally {release(jedis, isBroken);}return null;}protected List<String> getListFromJedis(String key, String methodName) throws JMSCacheException {List<String> list = null;boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();jedis.select(getDBIndex());if (jedis.exists(key)) {list = jedis.lrange(key, 0, -1);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list != null ? list.size() : 0);}} catch (JedisException e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list != null ? list.size() : 0, e);} finally {release(jedis, isBroken);}return list;}protected Set<String> getSetFromJedis(String key, String methodName) throws JMSCacheException {Set<String> list = null;boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();jedis.select(getDBIndex());if (jedis.exists(key)) {list = jedis.smembers(key);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, list != null ? list.size() : 0);}} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, list != null ? list.size() : 0, e);} finally {release(jedis, isBroken);}return list;}protected Map<String, String> getHashMapFromJedis(String key, String methodName) {Map<String, String> hashMap = null;boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();jedis.select(getDBIndex());hashMap = jedis.hgetAll(key);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, hashMap != null ? hashMap.size() : 0);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, hashMap != null ? hashMap.size() : 0, e);} finally {release(jedis, isBroken);}return hashMap;}protected String getHashMapValueFromJedis(String key, String field, String methodName) {String value = null;boolean isBroken = false;Jedis jedis = null;try {jedis = this.getJedis();if (jedis != null) {jedis.select(getDBIndex());if (jedis.exists(key)) {value = jedis.hget(key, field);LogContext.instance().debug(logger_default, "succeed:" + methodName, key, field, value);}}} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, key, field, value, e);} finally {release(jedis, isBroken);}return value;}public Long getIdentifyId(String identifyName ,String methodName) {boolean isBroken = false;Jedis jedis = null;Long identify=null;try {jedis = this.getJedis();if (jedis != null) {jedis.select(getDBIndex());identify = jedis.incr(identifyName);if(identify==0){return jedis.incr(identifyName);}else {return identify;}}} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:" + methodName, identifyName, "", identify, e);} finally {release(jedis, isBroken);}return null;}/*** 删除某db的某个key值* @param key* @return*/public Long delKeyFromJedis(String key) {boolean isBroken = false;Jedis jedis = null;long result = 0;try {jedis = this.getJedis();jedis.select(getDBIndex());LogContext.instance().debug(logger_default, "succeed:delKeyFromJedis");return jedis.del(key);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:delKeyFromJedis", e);} finally {release(jedis, isBroken);}return result;}/*** 根据dbIndex flushDB每个shard** @param dbIndex*/public void flushDBFromJedis(int dbIndex) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(dbIndex);jedis.flushDB();LogContext.instance().debug(logger_default, "succeed:flushDBFromJedis");} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:flushDBFromJedis", e);} finally {release(jedis, isBroken);}}public boolean existKey(String key, String methodName) {Jedis jedis = null;boolean isBroken = false;try {jedis = this.getJedis();jedis.select(getDBIndex());LogContext.instance().debug(logger_default, "succeed:"+methodName);return jedis.exists(key);} catch (Exception e) {isBroken = true;LogContext.instance().warn(logger_failure, "failed:"+methodName, e);} finally {release(jedis, isBroken);}return false;}}

 

转载于:https://www.cnblogs.com/daxiongblog/p/6515064.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/456285.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于response格式转换

调用图灵机器人api实例&#xff1a; class RobotHandler(WebSocketHandler):def open(self):# print("WebSocket opened",self.request.remote_ip)robot_set.add(self)# 获取websocket的发过来的信息def on_message(self, message):urlhttp://openapi.tuling123.com/…

微软老兵 Antoine LeBlond 将正式离职

Antoine LeBlond (安东勒布朗)是微软众高管之一,他在微软工作将近25年之久,然而他将在下周一,也就是他在微软的第9000天正式离职. 在发给微软众同事的邮件中,勒布朗表示他希望"探索微软之外的世界". 勒布朗在微软Office部门度过了他在微软的前20年时光,并与前微软高管…

转载——java synchronized详解

文章来源&#xff1a;http://www.cnblogs.com/GnagWang/archive/2011/02/27/1966606.html转载于:https://www.cnblogs.com/insist8089/p/6515885.html

Django中的F对象和Q对象

F对象 可以获取到自己的属性值实现自己的属性自己的属性关联的复杂条件支持运算 Q对象 Q对象实际上是对条件的一个封装封装后支持逻辑运算与或非 &|~ 支持嵌套 例子 from django.db.models import Max, Avg, F, Q from django.http import HttpResponse from django.s…

总链接

字符集修改、Linux时间同步、调整文件描述符http://11815879.blog.51cto.com/11805879/1915276正则表达式&#xff1a;http://11815879.blog.51cto.com/11805879/1919777 文件系统inode与blockhttp://11815879.blog.51cto.com/11805879/1917068 文件类型与软硬链接&#xff1a;…

Django模型关系

模型关系 1:1 一对一 &#xff08;一个身份证对应一个驾照&#xff09; 是使用外键唯一约束实现的对应最多只能有一个我们通常会在从表中声明关系 主表&#xff0c;从表 主表数据删除&#xff0c;从表数据级联删除从表数据删除&#xff0c;主表不受影响谁声明关系&#xff0c…

Android常用开源项目

Android常用开源项目 Android 2014-05-23 16:39:43 发布您的评价: 4.3 收藏 24收藏Android开源项目第一篇——个性化控件(View)篇包括ListView、ActionBar、Menu、ViewPager、Gallery、GridView、ImageView、ProgressBar、TextView、其他Android开源项目第二篇——工具库…

Django中数据知识点归纳

Django对象的增删改查 我们为了对django对象的增删改查进行总结&#xff0c;先在model.py文件中创建类便于举例 定义学生&#xff1a; class Students(models.Model):snamemodels.CharField(max_length20)sgendermodels.BooleanField(defaultTrue)sagemodels.IntegerField()…

面 试 细 节 一 点 通

面谈的礼节是社会新人及求职者踏人社会工作前最重要且最需学习的课题&#xff0c;因为这关系到是否能顺利踏入社会且寻找到一份合适满意的工作。 一个社会新人除了应注意的面试礼节外&#xff0c;在开始进行面谈之前及面谈结束之后&#xff0c;还有不少必须注意的礼仪。 面谈时…

宽带与流量的关系

流量&#xff0c;一般指的是每秒钟流经某设备的数据的多少。也就是Byte/Second( 字节每秒)。 比方说1M&#xff0c;这个概念的单位叫bPS(bit Per Second)比特每秒。而事实上经常用另外一个词来代替描述&#xff0c;也就是带宽。 而带宽和流量的换算关系是&#xff1a; 1 By…

PHP函数处理方法总结

call_user_func_array (PHP 4 > 4.0.4, PHP 5, PHP 7) call_user_func_array — 调用回调函数&#xff0c;并把一个数组参数作为回调函数的参数 说明 mixed call_user_func_array ( callable $callback , array $param_arr ) 把第一个参数作为回调函数&#xff08;callback&…

Django删除多对多表关系 :

删除多对多表关系 &#xff1a; # 删除子表与母表关联关系,让小虎不喜欢任何颜色 # 写法1: child_obj Child.objects.get(name"apollo") colors_obj Colors.objects.all() child_obj.favor child_obj.save() # 写法2: child_obj Child.objects.get(name"apo…

git push/pull时总需要输入用户名密码的解决方案

在提交项目代码或者拉代码的时候&#xff0c;git会让你输入用户名密码&#xff0c;解决方案&#xff1a;&#xff08;我们公司用的是gitlab&#xff09;执行git config --global credential.helper store命令然后git push origin your-branch会让你输入用户名和密码&#xff0c…

Django源代码写DetailView与ListView

基于类的通用视图 - 展平索引 通用显示视图 以下两个通用的基于类的视图旨在显示数据。在许多项目中&#xff0c;它们通常是最常用的视图。 一、DetailView django.views.generic.detail.DetailView 在执行此视图时&#xff0c;self.object将包含视图正在操作的对象。 此视图…

开源商务智能软件Pentaho

1 简介Pentaho是世界上最流行的开源商务智能软件,以工作流为核心的&#xff0c;强调面向解决方案而非工具组件的&#xff0c;基于java平台的商业智能(Business Intelligence,BI)套件BI&#xff0c;之所以说是套件是因为它包括一个web server平台和几个工具软件&#xff1a;报表…

chrome用type=file获取图片路径并转base64字符串

1 html页面 <div class"col-sm-2" style"height: 200px;margin-top: 14px;"> <input id"photo" name" " type"file" value"选择图片" ng-model"photoUrl"> <input type"button&qu…

Python - Django - 中间件 process_exception

process_exception(self, request, exception) 函数有两个参数&#xff0c;exception 是视图函数异常产生的 Exception 对象 process_exception 函数的执行顺序是按照 settings.py 中设置的中间件的顺序的倒序执行 process_exception 函数只在视图函数中出现异常的时候才执行…

NTV Media Server G3性能测试

Hello&#xff01;大家好&#xff0c;我是资深测试工程师Jackie&#xff0c;今天我来和大家一起对云视睿博的高性能流媒体服务器NTV Media Server G3做一次性能测试。今天测试有一个小目标&#xff0c;那就是验证在一台普通的PC机上&#xff0c;NTV Media Server G3的并发能力是…

人生不只是上坡路

从前的自己都是非常顺利的&#xff0c;觉得自己有一天一定可以成就一番事业。 可是谁也预料不到这几年的变化&#xff0c; 似乎人生就要跌落到了谷底&#xff0c; 不知道该如何去面对&#xff0c; 压力很大、惶恐不安、不知道未来的路该如何去走。 人生不只是上坡路&#xff0c…

Django 时间与时区设置问题

Django 时间与时区设置问题 在Django的配置文件settings.py中&#xff0c;有两个配置参数是跟时间与时区有关的&#xff0c;分别是TIME_ZONE和USE_TZ 如果USE_TZ设置为True时&#xff0c;Django会使用系统默认设置的时区&#xff0c;即America/Chicago&#xff0c;此时的TIME_…