Redis Pool--Java

配置文件

#redis conf

ADDR=127.0.0.1
PORT=
AUTH= #session timeout
TIMEOUT= MAX_ACTIVE=
MAX_IDLE=
MIN_IDLE=
MAX_WAIT=

RedisPool.java

 public final class RedisPool {

     private static JedisPool jedisPool = null;

     // 最大连接数:可同时建立的最大链接数
private static int max_acti; // 最大空闲数:空闲链接数大于maxIdle时,将进行回收
private static int max_idle; // 最小空闲数:低于minIdle时,将创建新的链接
private static int min_idle; // 最大等待时间:单位ms
private static int max_wait; private static String addr; private static int port; private static String auth; // session timeout by seconds
private static int session_timeout; private RedisPool() {
} /**
* get properties and init RedisPool
*/
static {
Properties pps = new Properties();
InputStream in;
try {
in = new BufferedInputStream(new FileInputStream("src"+File.separator+"conf"+File.separator+"redispool.properties"));
pps.load(in); addr = pps.getProperty("ADDR");
auth = pps.getProperty("AUTH");
port = Integer.parseInt(pps.getProperty("PORT"));
session_timeout = Integer.parseInt(pps.getProperty("TIMEOUT"));
max_acti = Integer.parseInt(pps.getProperty("MAX_ACTIVE"));
max_idle = Integer.parseInt(pps.getProperty("MAX_IDLE"));
min_idle = Integer.parseInt(pps.getProperty("MIN_IDLE"));
max_wait = Integer.parseInt(pps.getProperty("MAX_WAIT")); JedisPoolConfig config = new JedisPoolConfig();
config.setMaxActive(max_acti);
config.setMaxIdle(max_idle);
config.setMaxWait(max_wait);
config.setMinIdle(min_idle);
jedisPool = new JedisPool(config, addr, port, 1000, auth); } catch (Exception e) {
throw new RuntimeException("jedisPool init error" + e.getMessage());
} } /**
* get jedis resource
*/
public synchronized static Jedis getJedis() {
if (jedisPool != null) {
return jedisPool.getResource();
} else {
throw new RuntimeException("jedisPool is null");
}
} /**
* get value map by key
*
* @param key
* @return map
*/
public static Map<String, String> getHashValue(String key) {
Jedis jedis = getJedis();
Map<String, String> map = new HashMap<String, String>();
Iterator<String> iter = jedis.hkeys(key).iterator();
while (iter.hasNext()) {
String mkey = iter.next();
map.put(mkey, jedis.hmget(key, mkey).get(0));
}
jedisPool.returnResource(jedis);
return map;
} /**
* set value by key and map value
*
* @param key
* @param hash
*/
public static void setHashValue(String key, Map<String, String> hash) {
Jedis jedis = getJedis();
jedis.hmset(key, hash);
jedis.expire(key, session_timeout);
jedisPool.returnResource(jedis);
} /**
* remove value by key
*
* @param key
*/
public static void remove(String key) {
Jedis jedis = getJedis();
jedis.del(key);
jedisPool.returnResource(jedis);
} /**
* expire session time to session_timeout
*
* @param key
*/
public static void expire(String key) {
Jedis jedis = getJedis();
jedis.expire(key, session_timeout);
jedisPool.returnResource(jedis);
} /**
* return jedis resource
*/
public static void returnResource(final Jedis jedis) {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
} }

redis pool的更多相关文章

  1. Jedis(Java+Redis) Pool的使用

    今天试了一下Jedis里连接池JedisPool的的使用.代码如下: package com.myapp.jedis.pooldemo; import redis.clients.jedis.Jedi ...

  2. redis pool config的配置参数

    .获取jedis实例时,实际上可能有两类错误.一类是pool.getReource(),得不到可用的jedis实例:另一类是jedis.set/get时出错也会抛出异常:为了实现区分,所以根据inst ...

  3. redis.Pool 配置

    http://blog.csdn.net/xiaohu50/article/details/51606349

  4. springboot自动装配redis在pool下偶尔出现连接异常的问题

    jedis pool的配置其实是采用 org.apache.commons.pool2.impl.GenericObjectPoolConfig类的配置项. jedis 2.9版本代码如下: pack ...

  5. 缓存、队列(Memcached、redis、RabbitMQ)

    本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...

  6. Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  7. Day12-mysql&&redis

    1. 数据库介绍 什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数 ...

  8. redis 间断性耗时长问题解决

    我发现开发项目用的redis 隔一两分钟就出现 耗时问题,长达五秒.一开始以为是 redis 服务器不稳定,但运维测试发现redis稳定的,在高并发下最大耗时也就只有100毫秒左右,怎么也不可能达到5 ...

  9. python之redis和memcache操作

    Redis 教程 Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据 ...

随机推荐

  1. ajax常用参数

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址.前台跳转到后台 请求参数:前台向后台传数据 回调函数:回调函数就是一个自定义的函数在发生特定的事件的时候调用来处理这个事件 ...

  2. HTML输入框点击内容消失

    在input标签中这样写 type='text' onfocus='if(this.value=='请输入内容以搜索') this.value=''' onblur='if(this.value==' ...

  3. $.extend,$.fn.extend,$.fn的区别

    jQuery.extend(object) 为jQuery类添加类方法,可以理解为添加静态方法.如: jQuery.extend({ min: function(a, b) { return a &l ...

  4. 【海岛帝国系列赛】No.4 海岛帝国:LYF的太空运输站

    50212228海岛帝国:LYF的太空运输站 [试题描述] 最近,“购物券”WHT在“药师傅”帝国资源大会上提出了“SSTS”太空运输站计划.由于恐怖分子前些日子刚猖狂完,炸毁高楼无数,YSF不得不执 ...

  5. 视频处理控件TVideoGrabber视频捕捉设设备相关问题

    选择一个视频捕捉设备 首先设置 VideoSource = vs_VideoCaptureDevice来选择一个视频捕捉设备作为一个视频源. 通过指定VideoDevice属性来选择当前的视频捕捉设备 ...

  6. const修饰虚函数

    [1]程序1 #include <iostream> using namespace std; class Base { public: ; }; class Test : public ...

  7. semantic-ui and IE only message

    <![if !IE]> <div class="ui message red"> <i class="close icon"> ...

  8. linux 下某个文字在某几行的shell 写法 。

    cat -n  139.sql |grep "kkkn"  sed -n '697804,697812p' 139.sql

  9. crontab实现每秒执行

    crontab: #!/bin/bash step=$1 #每多少秒执行一次 ; i < ; i=(i+step) )); do date +%Y%m%d' '%H:%M:%S >> ...

  10. 怎么使用PHP获取用户客户端真实IP的解决方案呢?

    function getIp(){if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIE ...