關于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.pswp.cn/news/456285.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/456285.shtml
英文地址,請注明出處:http://en.pswp.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_…