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. Java实现文件的读写,复制

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStr ...

  2. haskell笔记1

    haskell platform下载:https://www.haskell.org/platform/ 进入haskell控制台,终端输入 $ ghci 编译文件 :l file.hs 数组操作 & ...

  3. ionic入门之AngularJS扩展基本布局

    目录: 标题栏 : ion-header-bar 页脚栏 : ion-footer-bar header/footer : 样式及内容 内容区 : ion-content 滚动框 : ion-scro ...

  4. maven相关

    1. 创建/导入maven项目时 eclipse默认jdk版本配置:http://blog.csdn.net/lzj0470/article/details/42292021 2. eclipse 使 ...

  5. Sed文本替换一例

    使用 Sed 完成文本替换操作任务是非常合适的. 现在, 假设我要将一个原有 Java 项目中的一些包及下面的类移到另一个项目中复用. Project javastudy: Packages: alg ...

  6. access数据库导入Oracle

    1.对着当前的表右击->导出->选择下面的保存类型为"ODBC数据库"找一个路径输入文件名2.将x导出到x,点击->确定3.在弹出的对话框中DSN名称,点击-&g ...

  7. Linux内核调试方法总结【转】

    转自:http://my.oschina.net/fgq611/blog/113249 内核开发比用户空间开发更难的一个因素就是内核调试艰难.内核错误往往会导致系统宕机,很难保留出错时的现场.调试内核 ...

  8. 收缩 虚拟硬盘 shrink vhd

    在使用WIN2012 的Hyper-v的虚拟磁盘时, 有时需要将磁盘中未使用的控件收缩掉, 这时就需要使用Hyper-v磁盘工具的收缩功能. 如果使用Hyper-v磁盘工具, 不能对vhd虚拟磁盘进行 ...

  9. Hibernate,JPA注解@OneToMany_Set

    用例代码如下: 数据库DDL语句 1,CAT表 create table CAT ( id CHAR) not null, create_time ), update_time ), cat_name ...

  10. Shipyard安装、使用

    Shipyard使用Citadel集群管理工具包,简化对横跨多个主机的Docker容器集群进行管理.通过Web用户界面,你可以大致浏览相关信息,比如你的容器在使用多少处理器和内存资源.在运行哪些容器, ...