redis pool

时间:2023-01-01 13:46:13

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&lpar;Java&plus;Redis&rpar; 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&period;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&amp&semi;&amp&semi;redis

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

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

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

  9. python之redis和memcache操作

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

随机推荐

  1. Scalaz(37)- Free :实践-DB Transaction free style

    我一直在不断的提示大家:FP就是Monadic Programming,是一种特殊的编程风格.在我们熟悉的数据库编程领域能不能实现FP风格呢?我们先设计一些示范例子来分析一下惯用的数据库编程过程: i ...

  2. iOS— UIScrollView和 UIPageControl之间的那些事

    本代码主要实现在固定的位置滑动图片可以切换. 目录图如下: ViewController.h #import <UIKit/UIKit.h> // 通过宏定义定义宽和高 #define W ...

  3. C语言中,宏和全局变量的区别是什么?

    全局变量 是可以在程序中任何地方使用 而且是可以修改的 宏定义也可以在任何地方使用 但是不能在之后修改 数据类型没有限制的 宏的例子:#define 宏名 宏体 #define PI 3.141592 ...

  4. 学习资料 50个常用的sql语句

    Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题 ...

  5. PHP漏洞全解&lpar;四&rpar;-xss跨站脚本攻击

    本文主要介绍针对PHP网站的xss跨站脚本攻击.跨站脚本攻击是通过在网页中加入恶意代码,当访问者浏览网页时恶意代码会被执行或者通过给管理员发信息 的方式诱使管理员浏览,从而获得管理员权限,控制整个网站 ...

  6. eclipse tomcat内存设置

    -Xms256M -Xmx512M -XX:PermSize=256m -XX:MaxPermSize=512m

  7. SpringBoot整合RabbitMQ-服务安装

    本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...

  8. VC&plus;&plus;6&period;0 add files to project 造成Visual Studio崩溃的解决方法

    1.下载filetool.exe,然后将文件解压在一个小文件夹内2.打开filetool.dsw 在release模式下编译程序,复制filetool.dll3.放在VC6.0安装目录AddIns的下 ...

  9. javascript面向对象学习

    1.this指向问题,指向的是当前的方法属于谁,当前的方法属于谁就指向谁!! 例子: oDiv.onclick = function () { this指向的是oDiv,因为这个方法属于oDiv } ...

  10. Java写 插入 选择 冒泡 快排

    /** * Created by wushuang on 2014/11/19. */ public class SortTest { @Test public void mainTest() { i ...