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= #最大空闲 ...
随机推荐
- cloudera learning7:Hadoop资源管理
Linux Control Groups(cgroups):在操作系统级别进行资源分配,可通过Cloudera Static Service Pools配置. YARN调度器配置:对运行在YARN上的 ...
- JAVASE02-Unit06: 文件操作——File 、 文件操作—— RandomAccessFile
Unit06: 文件操作--File . 文件操作-- RandomAccessFile java.io.FileFile的每一个实例是用来表示文件系统中的一个文件或目录 package day06; ...
- 原生node的header
首先引入http模块 获取http.ServerResponse对象的方式,1.http.createServer(function(req,res){}) 其中res是http.ServerResp ...
- Python之路-python(css布局、JavaScript)
CSS布局 JavaScript css布局: 后台管理界面一:(左右标签都有下来菜单) 利用position: absolute;让某个标签固定在具体位置,然后使用overflow: auto;属性 ...
- hadoop_elk架构图
- 安装完magento后,其他电脑无法访问magento,URL自动跳转到http://localhost/magento
问题:在电脑A上安装完了magento 1.7.0.2 然后, 在电脑A上用 http://localhost/magento 访问网站,没有问题. 但在电脑B 上用 http://192.168.4 ...
- mac brew 安装php扩展报错:parent directory is world writable but not sticky
$ brew install php70-mcrypt 报错: Error: parent directory is world writable but not sticky 搜索到github的答 ...
- Hessian怎样实现远程调用
1.Spring中除了提供HTTP调用器方式的远程调用,还对第三方的远程调用实现提供了支持,其中提供了对Hessian的支持. Hessian是由Caocho公司发布的一个轻量级的二进制协议远程调用实 ...
- Log(android.util.Log)(option+return)
Log.v() verbose 琐碎,详细 Log.d() debug 调试 Log.i() info 信息,重要,分析行为 Log.w() wain 警告 log.e() error 错误 参数:t ...
- linux下JsonServer启动
1:进入到JsonServer run.sh目录下; 2:执行"export PATH=.:$PATH"; 3:执行"run.sh start"; 这样便把Js ...