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缓存连接池管理的更多相关文章

  1. nodejs + redis/mysql 连接池问题

    nodejs + redis/mysql 连接池问题 需不需要连接池 连接池的作用主要是较少每次临时建立连接所带来的开销.初步一看,nodejs运行单线程上,它不能同时使用多个连接,乍一看是不需要连接 ...

  2. python redis之连接池的原理

    python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...

  3. 关于.NET大数据量大并发量的数据连接池管理

    转自:http://www.cnblogs.com/virusswb/archive/2010/01/08/1642055.html 我以前对.NET连接池的认识是错误的,原来以为在web.confi ...

  4. redis运用连接池报错解决

    redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...

  5. Spring学习11-Spring使用proxool连接池 管理数据源

    Spring 一.Proxool连接池简介及其配置属性概述   Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是 ...

  6. Spring使用proxool连接池 管理数据源

    一.Proxool连接池简介及其配置属性概述 Proxool是一种Java数据库连接池技术.是sourceforge下的一个开源项目,这个项目提供一个健壮.易用的连接池,最为关键的是这个连接池提供监控 ...

  7. Redis Java连接池调研

    Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...

  8. HttpPoolUtils 连接池管理的GET POST请求

    package com.nextjoy.projects.usercenter.util.http; import org.apache.http.Consts; import org.apache. ...

  9. redis单机连接池

    一.配置文件 1. db.properties配置文件#IP地址 redis.ip = 127.0.0.1 #端口号 redis.port= #最大连接数 redis.max.total= #最大空闲 ...

随机推荐

  1. Chrome 中的 JavaScript 断点设置和调试技巧

    Console:此功能是模拟js控制台,直接写代码,查看结果.高级功能使用时开启断点,查看变量的变化过程.还可以条用函数. Resources:次功能是查看加载页面所用的资源,链接的数据库,域名下保存 ...

  2. visual studio2015使用git管理源代码

    1.注册https://git.oschina.net/ 2.注册好后,创建一个测试项目,如下图: 点击创建,如下: 上面的红框中的地址下面会用到. 3.git初始化设置 在本地电脑要安装git,打开 ...

  3. 开启JMX功能,使JVisvualVM能够连接JVM

    -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.manageme ...

  4. nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 错误解决

    今天在做LNMP的时候,启动nginx服务,无法开启,导致网页打不开.把服务从起一下发现提示错误如下: Starting nginx: nginx: [emerg] bind() to 0.0.0.0 ...

  5. ReadMe.md MarkDown file

    MarkDown 文件写法类似写Confluence page. http://blog.csdn.net/kaitiren/article/details/38513715

  6. JDBC数据库1

    数据库(Database)是按照数据结构来组织,存储,和管理数据的仓库.数据库有很多类型,从简单存储各种数据的的表格到能够储存大型数据的系统,在各方面得到了广泛的应用.数据库简介J.Martin给 数 ...

  7. 003:Posix IPC的消息队列

    1:与FIFO相比,FIFO要求对一个管道写入之前,必须有进程进行读打开.消息队列则不需要有进行在队列上等待消息的到达. 2:POSIX每次读取总是返回优先级最高的,system V则可以返回任意优先 ...

  8. Linux -- objdump二进制文件比较

    objdump工具用来显示二进制文件的信息,就是以一种可阅读的格式让你更多地了解二进制文件可能带有的附加信息. 常用参数说明 -f 显示文件头信息 -D 反汇编所有section (-d反汇编特定se ...

  9. easyUI 复选框批量操作

    前台js代码 function destroyExcelout(){ //返回选中多行              var row = $('#dg').datagrid('getSelections' ...

  10. hadoop的学习

    http://www.aboutyun.com/thread-6179-1-1.html