博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Redis缓存连接池管理
阅读量:5125 次
发布时间:2019-06-13

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

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.Assert; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.exceptions.JedisException; import java.util.*; /**  * redis缓存管理  * Created by leichaoxiong on 2016/7/6.  */ public class RedisProvider {
protected static final Logger LOG = LoggerFactory.getLogger(RedisProvider.class); protected static JedisPool jedispool; protected static int EXPIRE = 130; static{
ResourceBundle bundle = ResourceBundle.getBundle("redis"); if (bundle == null) {
throw new IllegalArgumentException( "[redis.properties] is not found!"); } EXPIRE = Integer.valueOf(bundle.getString("redis.expire")); JedisPoolConfig jedisconfig = new JedisPoolConfig(); jedisconfig.setMaxActive(Integer.valueOf(bundle .getString("redis.pool.maxActive"))); jedisconfig.setMaxIdle(Integer.valueOf(bundle .getString("redis.pool.maxIdle"))); jedisconfig.setMaxWait(Long.valueOf(bundle .getString("redis.pool.maxWait"))); jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle .getString("redis.pool.testOnBorrow"))); jedisconfig.setTestOnReturn(Boolean.valueOf(bundle .getString("redis.pool.testOnReturn"))); jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")), 100000); } public static Jedis getJedis() {
boolean broken= false; Jedis jedis = null; try {
jedis = jedispool.getResource(); } catch (JedisException je) {
// 是否销毁与回收对象 broken = handleJedisException(je); }finally {
// 回收到连接池 closeResource(jedis,broken); } return jedis; } public static void returnResource(JedisPool pool, Jedis jedis) {
if (jedis != null) {
pool.returnResource(jedis); } } /** * 读取所有key * @return */ public static Set getKeys(){
Set set = null; try{
set = getJedis().keys("*"); }catch (Exception e){
} return set; } public static String get(String key){
String str = null; try{
str = getJedis().get(key); }catch (Exception e){
} return str; } /** * 模糊key查询 * @param key * @return */ public static Set getKeys(String key){
Set set = null; try{
set = getJedis().keys(key+"*"); }catch (Exception e){
} return set; } /** * Map集合 * @param key * @param map */ public static void hmSet(String key,Map map){
try{
getJedis().hmset(key,map); }catch (Exception e){
e.printStackTrace(); } } /** * 缓存List * @param key * @param value */ public static void addList(String key,String... value){
try{
getJedis().rpush(key,value); }catch (Exception e){
e.printStackTrace(); } } /** * 全取List * @param key * @return */ public static List
getList(String key){
try{
return getJedis().lrange(key,0,-1); }catch (Exception e){
e.printStackTrace(); } return null; } /** * 读取单个Key集合 * @param key * @param mvalue * @return */ public static List
hmGet(String key,String ... mvalue){
List
strings = null; try{
strings = getJedis().hmget(key,mvalue); }catch (Exception e){
e.printStackTrace(); } return strings; } /** * set 键值 * @param key * @param value */ public static void set(String key, String value){
try{
getJedis().set(key,value); }catch (Exception e){
} } /** * 带过期时间 * @param key * @param time * @param value */ public static void set(String key,int time, String value){
try{
getJedis().setex(key,time,value); }catch (Exception e){
} } /** * 删除指定key * @param key */ public static void del(String... key){
try{
getJedis().del(key); }catch(Exception e){
} } public static void batchDel(String key){
try{
Set
set = getJedis().keys(key +"*"); Iterator
it = set.iterator(); while(it.hasNext()){
String keyStr = it.next(); // System.out.println(keyStr); getJedis().del(keyStr); } }catch(Exception e){
} } /** * 删除map中指定值 * @param key * @param mvalue */ public static void hDel(String key,String... mvalue){
try{
getJedis().hdel(key,mvalue); }catch(Exception e){
} } protected static boolean handleJedisException(JedisException jedisException) {
if (jedisException instanceof JedisConnectionException) {
LOG.error("Redis connection lost.", jedisException); } else if (jedisException instanceof JedisDataException) {
if ((jedisException.getMessage() != null) && (jedisException.getMessage().indexOf("READONLY") != -1)) {
LOG.error("Redis connection are read-only slave.", jedisException); } else {
// dataException, isBroken=false return false; } } else {
LOG.error("Jedis exception happen.", jedisException); } return true; } protected static void closeResource(Jedis jedis, boolean conectionBroken) {
try {
if (conectionBroken) {
jedispool.returnBrokenResource(jedis); } else {
jedispool.returnResource(jedis); } } catch (Exception e) {
LOG.error("return back jedis failed, will fore close the jedis.", e); } } }

转载于:https://www.cnblogs.com/itjiandan/p/6248788.html

你可能感兴趣的文章
electron知识点
查看>>
字符串json转换为xml xml转换json
查看>>
C#多线程编程
查看>>
投资股权众筹项目,至少需要关注6个方面
查看>>
网站开发和企业级开发有什么区别?
查看>>
北漂周记--第2记--培训开始
查看>>
一个伟大计划终于完成了(粉丝联盟网正式上线了)
查看>>
设计模式 学习笔记(2)单一职责原则、开放封闭原则、依赖倒转原则
查看>>
Thread类和Runnable接口
查看>>
【Vue】【Router】手动跳转用 this.$router.push() 时 $router 未定义的问题
查看>>
方法的重载(overload)和重写(override)的区别
查看>>
iReport报表工具的使用
查看>>
hdoj-3342-Legal or Not(拓扑排序)
查看>>
第三章 使用属性升级MyBank
查看>>
Linux实用指令
查看>>
Linux软链接和硬链接
查看>>
可持久化线段树学习笔记
查看>>
System
查看>>
mac下用xattr命令来删除文件的扩展属性
查看>>
jQ实现JSON.stringify(obj)方法
查看>>