Redis缓存连接池管理
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<String> 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<String> hmGet(String key,String ... mvalue){
List<String> 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<String> set = getJedis().keys(key +"*");
Iterator<String> 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);
}
}
}
Redis缓存连接池管理的更多相关文章
- nodejs + redis/mysql 连接池问题
nodejs + redis/mysql 连接池问题 需不需要连接池 连接池的作用主要是较少每次临时建立连接所带来的开销.初步一看,nodejs运行单线程上,它不能同时使用多个连接,乍一看是不需要连接 ...
- python redis之连接池的原理
python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...
- 关于.NET大数据量大并发量的数据连接池管理
转自:http://www.cnblogs.com/virusswb/archive/2010/01/08/1642055.html 我以前对.NET连接池的认识是错误的,原来以为在web.confi ...
- redis运用连接池报错解决
redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...
- Spring学习11-Spring使用proxool连接池 管理数据源
Spring 一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是 ...
- Spring使用proxool连接池 管理数据源
一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...
- Redis Java连接池调研
Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...
- HttpPoolUtils 连接池管理的GET POST请求
package com.nextjoy.projects.usercenter.util.http; import org.apache.http.Consts; import org.apache. ...
- redis单机连接池
一.配置文件 1. db.properties配置文件#IP地址 redis.ip = 127.0.0.1 #端口号 redis.port= #最大连接数 redis.max.total= #最大空闲 ...
随机推荐
- C++中为什么要将析构函数定义成虚函数
构造函数不可以是虚函数的,这个很显然,毕竟虚函数都对应一个虚函数表,虚函数表是存在对象内存空间的,如果构造函数是虚的,就需要一个虚函数表来调用,但是类还没实例化没有内存空间就没有虚函数表,这根本就是个 ...
- ubuntu 上安装字体
http://www.360doc.com/content/11/0901/23/4171006_145128703.shtml http://www.linuxidc.com/Linux/2008- ...
- visual studio2015从git上clone(克隆)项目
本文介绍Visual Studio2015从git上clone项目代码的步骤. 1.打开VS2015,进到起始页,打开"团队资源管理器",如下图: 2.点击"克隆&quo ...
- ui-grid
html代码: <html ng-app="myApp"> <head> <meta charset="utf- ...
- 一些NSArray,NSDictionary,NSSet相关的算法知识
iOS编程当中的几个集合类:NSArray,NSDictionary,NSSet以及对应的Mutable版本,应该所有人都用过.只是简单使用的话,相信没人会用错,但要做到高效(时间复杂度)精确(业务准 ...
- sys.stdout.write与sys.sterr.write(一)
目标: 1.使用sys.stdout.write输入0-9数字 2.使用sys.stderr.write输出0-9数字 3.使用两种方式输出0-9,显示0变化到9的过程 1.使用sys.stdout. ...
- iOS,应用崩溃日志分析
参考资料:http://www.cocoachina.com/industry/20130725/6677.html 1.获得崩溃日志 2.崩溃日志实例 3.符号化崩溃日志 4.低内存闪退 获得崩溃日 ...
- Node.js 安装与配置
引言: JavaScript是一种运行在浏览器的脚本,它简单,轻巧,易于编辑,这种脚本通常用于浏览器的前端编程,但是一位开发者Ryan有一天发现这种前端式的脚本语言可以运行在服务器上的时候,一场席卷全 ...
- 建模分析之机器学习算法(附python&R代码)
0序 随着移动互联和大数据的拓展越发觉得算法以及模型在设计和开发中的重要性.不管是现在接触比较多的安全产品还是大互联网公司经常提到的人工智能产品(甚至人类2045的的智能拐点时代).都基于算法及建模来 ...
- PAT——乙级真题1001代码