Redis Pool--Java

配置文件

#redis confADDR=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 }

转载于:https://www.cnblogs.com/luangeng/p/5767531.html

redis pool相关推荐

  1. Redis pool 配置详解

    JedisPool的配置参数大部分是由JedisPoolConfig的对应项来赋值的. maxActive:控制一个pool可分配多少个jedis实例,通过pool.getResource()来获取: ...

  2. redis java切片_jedis 单点配置

    pom引入jar包 redis.clients jedis 2.9.0 spring 配置 type="int" /> redis.properties配置文件 redis. ...

  3. springboot @cacheable不起作用_Springboot学习记录13 使用缓存:整合redis

    本学习记录的代码,部分参考自gitee码云的如下工程.这个工程有详尽的Spingboot1.x教程.鸣谢! https://gitee.com/didispace/SpringBoot-Learnin ...

  4. Spring Boot 整合Redis 实现缓存

    本文提纲 一.缓存的应用场景 二.更新缓存的策略 三.运行 springboot-mybatis-redis 工程案例 四.springboot-mybatis-redis 工程代码配置详解 运行环境 ...

  5. Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

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

  6. Windows下安装Mongodb SpringBoot集成MongoDB和Redis多数据源

    全文内容: Mongodb安装 说明:Mongodb和redis是开发中常用的中间件,Redis的安装使用比较简单就不写了,只说本地也就是Windows安装Mongodb. SpringBoot集成M ...

  7. springboot 整合redis 实现KeySpaceNotification 键空间通知

    2019独角兽企业重金招聘Python工程师标准>>> 目录结构如下: application.properties配置文件(redis的配置): spring.redis.host ...

  8. java实现redis缓存_java实现redis缓存功能

    一.安装redis 1.mac安装,如果有安装brew 可以直接快捷安装:brew install redis 2.linux下载安装wget http://download.redis.io/rel ...

  9. Spring Boot(十一)Redis集成从Docker安装到分布式Session共享

    2019独角兽企业重金招聘Python工程师标准>>> 一.简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并 ...

最新文章

  1. python语言标号_Python 编码为什么那么蛋疼?
  2. 程序清单3.3_bases.c程序_《C Primer Plus》P37
  3. invalid value encountered in double_scalars
  4. WINDOWS 蓝屏错误代码说明一览表
  5. HTML 中多媒体设置:1、滚动字幕及滚动图片的设置 2、音乐视频的插入
  6. javascript 解决IE8 兼容 placeholder 属性 含password
  7. (26)HTML兼容写法
  8. RabbitMQ架构
  9. 基于JAVA+SpringBoot+Mybatis+MYSQL的在线论坛管理系统
  10. liunx下pytorch(python2.7)先前几个版本的安装(由于官网点击先前版本进不去)
  11. itlwm驱动_GitHub - sjoye/itlwm: IntelWifi
  12. Winform软件,不要在线程里操作UI
  13. 压力测试 - Apache JMeter使用教程
  14. 一文看懂李录价值投资体系
  15. BLDC无刷电机驱动板,foc驱动板,有霍尔接口,反电动势接口,三相电流采集接口
  16. 程序员面试 算法研究 编程艺术 红黑树 机器学习5大系列集锦
  17. 1.7 URL与端点
  18. 记flume部署过程中遇到的问题以及解决方法(持续更新)
  19. 【python小练】0014题 和 0015 题
  20. poj 1789 kruscal水题

热门文章

  1. python 三元运算符求abc_python三元运算符实现方法
  2. python就业前景如何_2020年Python就业前景如何?就业岗位多不多?薪资高不高?...
  3. java pdf 书签_Java 展开或折叠PDF中的书签
  4. 每天一道LeetCode-----找到所有被某个字符包围的另一个字符
  5. 每天一道LeetCode-----买卖商品问题,计算最大利润,分别有一次交易,两次交易,多次交易的情况
  6. android开发启动画面,Android开发笔记——如何正确实现App启动页
  7. java(1)——用notepad++编译java(javac.exe)
  8. 在Linux虚拟机中添加多个ip地址
  9. vscode could not establish connection to linux The VS Code Server failed to start
  10. XP MSTSC连接WIN7或WIN8问题