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= #最大空闲 ...
随机推荐
- [原]关于flash GPU渲染的一些不完全测试(wmode,ie,chrome)
关于flash GPU渲染的一些不完全测试(wmode,ie,chrome) 测试环境: 随意取一段优酷超清视频,在本地以网页最大化方式播放,分辨率约1080P左右. 观察不同wmode值下,flas ...
- PHP访问带密码的Redis
1. 设置Redis密码,以提供远程登陆打开redis.conf配置文件,找到requirepass,然后修改如下: requirepass yourpassword yourpassword就是re ...
- Java提高篇——单例模式
介绍 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就是我们今天要 ...
- Java 使用GDAL 读写 shapefile
读取shp文件,并把它转化为json import org.gdal.ogr.*; import org.gdal.ogr.Driver; import org.gdal.gdal.*; public ...
- RDIFramework.NET框架Web中datagrid与treegrid控件自动生成右键菜单与列标题右键菜单
在实际应用中常可以看到数据展示控件有右键菜单的功能,对应的列标题也可以右键弹出快捷菜单设置指定列的显示与隐藏等功能.在我们的RDIFramework.NET Web框架中,只要是使用了EasyUI的D ...
- ko trick
(1)let a = ko.observable(‘A’)绑定到select,如果下拉列表中找不到'A', 变量a会赋值成undefined. 要解决此问题可以使用绑定valueAllowUnse ...
- Leetcode: Design Snake Game
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- <<小朋友的数字>>核心代码
for(i=l;i<=n;i++) { a[i]=read(); t=t+a[i]; if (t>maxn) maxn=t if( t<0) t=0; b[i]=maxn; }
- 使用Quicktime 实现视频直播(Live video using Quicktime) (转)
Quicktime是一个跨浏览器的播放插件,可以实现RTSP视频直播,可用于电视直播或视频监控平台.本文主要讲了关于播放器如何实现直播.事件响应.播放器全屏.动态修改播放路径等问题. 需要准备的软件: ...
- 如何查看oracle数据库的所有的关键字
管理员账户登录后,执行以下命令: select * from v$reserved_words 附上参考: NOMONITORINGRECORDS_PER_BLOCKCASCADEDYNAMIC_S ...