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. UVA 10498 Happiness(线性规划-单纯形)

    Description Prof. Kaykobad has given Nasa the duty of buying some food for the ACM contestents. Nasa ...

  2. 。。。JDBC里面的sql与hibernate里面的hql有关占位符"?"的总结。。。

    今天在看Hibernate的时候,似乎又有了一些收获的东东,嘻嘻... 我记得很清楚:以前用JDBC操作数据库的时候是这样的: String sql = "select * from use ...

  3. oracle热备份

    1:热备份: SHUTDOWN IMMEDIATE; STARTUP MOUNT; alter database archivelog; --ALTER SYSTEM SET LOG_ARCHIVE_ ...

  4. 夺命雷公狗---Thinkphp----5之数据库的链接

    我们打开WEB目录下发现了Common和Home以及Runtime这三个文件夹 那么我们第一个目标是完成网站后台的首页吧,那么我们就直接将Home的文件夹复制一份出来,并且改名为Admin这样就可以分 ...

  5. 锋利的JQuery(六)

    $.ajax():可以设定beforeSend.error.success.complete等 $.getScript():加载JS文件 $.getJSON():加载JSON文件 $.each():通 ...

  6. error CS0007: Unexpected common language runtime initialization error -- '没有注册类别 '

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework]"Inst ...

  7. PHPExcel读取excel文件示例

    PHPExcel读取excel文件示例PHPExcel最新版官方下载网址:http://phpexcel.codeplex.com/PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包 ...

  8. CDN流量放大攻击思路

    首先,为了对CDN进行攻击,我们必须清楚CDN的工作原理,这里我们再来简单介绍一下CDN的工作模型. CDN的全称是Content Delivery Network(内容分发网络),通过在网络各处的加 ...

  9. NLog

    C# 使用NLog记录日志 C#第三方日志库Nlog NLog类库使用探索——详解配置 使用Nlog记录日志到数据库 NLog文章系列——系列文章目录以及简要介绍 NLog日志框架简写用法(write ...

  10. 转:redis常用命令

    一 Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发 ...