redis pool

Redis Pool--Java

配置文件

#redis conf

ADDR=127.0.0.1
PORT=6379
AUTH=

#session timeout
TIMEOUT=1000  

MAX_ACTIVE=500
MAX_IDLE=200
MIN_IDLE=100
MAX_WAIT=1000

RedisPool.java

  1 public final class RedisPool {
  2
  3     private static JedisPool jedisPool = null;
  4
  5     // 最大连接数:可同时建立的最大链接数
  6     private static int max_acti;
  7
  8     // 最大空闲数:空闲链接数大于maxIdle时,将进行回收
  9     private static int max_idle;
 10
 11     // 最小空闲数:低于minIdle时,将创建新的链接
 12     private static int min_idle;
 13
 14     // 最大等待时间:单位ms
 15     private static int max_wait;
 16
 17     private static String addr;
 18
 19     private static int port;
 20
 21     private static String auth;
 22
 23     // session timeout by seconds
 24     private static int session_timeout;
 25
 26     private RedisPool() {
 27     }
 28
 29     /**
 30      * get properties and init RedisPool
 31      */
 32     static {
 33         Properties pps = new Properties();
 34         InputStream in;
 35         try {
 36             in = new BufferedInputStream(new FileInputStream("src"+File.separator+"conf"+File.separator+"redispool.properties"));
 37             pps.load(in);
 38
 39             addr = pps.getProperty("ADDR");
 40             auth = pps.getProperty("AUTH");
 41             port = Integer.parseInt(pps.getProperty("PORT"));
 42             session_timeout = Integer.parseInt(pps.getProperty("TIMEOUT"));
 43             max_acti = Integer.parseInt(pps.getProperty("MAX_ACTIVE"));
 44             max_idle = Integer.parseInt(pps.getProperty("MAX_IDLE"));
 45             min_idle = Integer.parseInt(pps.getProperty("MIN_IDLE"));
 46             max_wait = Integer.parseInt(pps.getProperty("MAX_WAIT"));
 47
 48             JedisPoolConfig config = new JedisPoolConfig();
 49             config.setMaxActive(max_acti);
 50             config.setMaxIdle(max_idle);
 51             config.setMaxWait(max_wait);
 52             config.setMinIdle(min_idle);
 53             jedisPool = new JedisPool(config, addr, port, 1000, auth);
 54
 55         } catch (Exception e) {
 56             throw new RuntimeException("jedisPool init error" + e.getMessage());
 57         }
 58
 59     }
 60
 61     /**
 62      * get jedis resource
 63      */
 64     public synchronized static Jedis getJedis() {
 65         if (jedisPool != null) {
 66             return jedisPool.getResource();
 67         } else {
 68             throw new RuntimeException("jedisPool is null");
 69         }
 70     }
 71
 72     /**
 73      * get value map by key
 74      *
 75      * @param key
 76      * @return map
 77      */
 78     public static Map<String, String> getHashValue(String key) {
 79         Jedis jedis = getJedis();
 80         Map<String, String> map = new HashMap<String, String>();
 81         Iterator<String> iter = jedis.hkeys(key).iterator();
 82         while (iter.hasNext()) {
 83             String mkey = iter.next();
 84             map.put(mkey, jedis.hmget(key, mkey).get(0));
 85         }
 86         jedisPool.returnResource(jedis);
 87         return map;
 88     }
 89
 90     /**
 91      * set value by key and map value
 92      *
 93      * @param key
 94      * @param hash
 95      */
 96     public static void setHashValue(String key, Map<String, String> hash) {
 97         Jedis jedis = getJedis();
 98         jedis.hmset(key, hash);
 99         jedis.expire(key, session_timeout);
100         jedisPool.returnResource(jedis);
101     }
102
103     /**
104      * remove value by key
105      *
106      * @param key
107      */
108     public static void remove(String key) {
109         Jedis jedis = getJedis();
110         jedis.del(key);
111         jedisPool.returnResource(jedis);
112     }
113
114     /**
115      * expire session time to session_timeout
116      *
117      * @param key
118      */
119     public static void expire(String key) {
120         Jedis jedis = getJedis();
121         jedis.expire(key, session_timeout);
122         jedisPool.returnResource(jedis);
123     }
124
125     /**
126      * return jedis resource
127      */
128     public static void returnResource(final Jedis jedis) {
129         if (jedis != null) {
130             jedisPool.returnResource(jedis);
131         }
132     }
133
134 }
时间: 2024-07-28 12:52:31

redis pool的相关文章

Jedis(Java+Redis) Pool的使用

今天试了一下Jedis里连接池JedisPool的的使用.代码如下: package com.myapp.jedis.pooldemo; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Created by baidu on 16/10/18. */ public class TestPool { pr

springboot自动装配redis在pool下的问题

jedis pool的配置其实是采用 org.apache.commons.pool2.impl.GenericObjectPoolConfig类的配置项. jedis 2.9版本代码如下: package redis.clients.jedis; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; public class JedisPoolConfig extends GenericObjectPoolConfig {

Python之路【第九篇】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度.Memcached基于一个存储键/值对的hashmap.其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信. Memc

使用Spring + Jedis集成Redis

一.集成环境 Tomcat7 JDK1.7 Jedis-2.7.2 Spring-4.1.6 二.资源依赖 (省略,网上很多) 三.集成过程 1.配置资源池 这里使用Jedis的ShardedJedisPool来管理,我们定义该配置文件为:spring-redis.xml,全部内容如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframew

剑指架构师系列-Redis集群部署

初步搭建Redis集群 克隆已经安装Redis的虚拟机,我们使用这两个虚拟机中的Redis来搭建集群. master:192.168.2.129 端口:7001 slave:192.168.2.132 端口:7002 sentinel:192.168.2.129 端口:26379 来说一下这个sentinel,sentinel是一个管理redis实例的工具,它可以实现对redis的监控.通知.自动故障转移.sentinel不断的检测redis实例是否可以正常工作,通过API向其他程序报告redi

封装redis(jedis)

1.添加pom文件<!-- Jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.2.1</version> </dependency> 2.添加redis.properties文件book.redis.pool.maxWait=1000book.redis.

Redis设置使用几号库

Redis中SpringBoot项目中的配置: 1.引入 spring-boot-starter-redis(POM.XML) <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> 2.添加配置文件(application.properties)

redis mysql 连接池 之 golang 实现

分享一下 golang 实现的 redis 和 mysql 连接池,可以在项目中直接引用连接池句柄,调用对应的方法. 举个栗子: 1 mysql 连接池的使用 (1) 在项目子目录放置 mysql.go (2)在需要调用的地方导入连接池句柄 DB (3)调用 DB.Query() 2 redis 连接池的使用 (1)在项目子目录放置 redis.go (2)在需要调用的地方导入连接池句柄 Cache (3)调用 Cache.SetString ("test_key", "te

Python自动化 【第十一篇】:Python进阶-RabbitMQ队列/Memcached/Redis

 本节内容: RabbitMQ队列 Memcached Redis 1.  RabbitMQ 安装 http://www.rabbitmq.com/install-standalone-mac.html 安装python rabbitMQ module pip install pika or easy_install pika or 源码 https://pypi.python.org/pypi/pika 实现最简单的队列 send 端 received 端 1.1 Work Queues 在这