redis单机连接池
一、配置文件
1. db.properties配置文件
#IP地址
redis.ip = 127.0.0.1
#端口号
redis.port=
#最大连接数
redis.max.total=
#最大空闲数
redis.max.idle=
#最小空闲数
redis.min.idle=
#效验使用可用连接
redis.test.borrow=true
#效验归还可用连接
redis.test.return=false 2. pom.xml文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
二、java代码
public class RedisPool {
private static JedisPool pool ; //jedis连接池
private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total","")); //最大连接数
private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","")); //最大空闲状态
private static Integer minIdle =Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle","")); //最小空闲状态
private static Boolean testOnBorrow =Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow","true")); //验证从连接池拿出的jedis实例,一定可用
private static Boolean testOnReturn =Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return","true")); //验证还回连接池的jedis实例,一定可用
private static String redisIp =PropertiesUtil.getProperty("redis.ip"); //最小空闲状态
private static Integer redisPort =Integer.parseInt(PropertiesUtil.getProperty("redis.port")); //最小空闲状态
private static void initPool(){
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMinIdle(minIdle);
config.setTestOnBorrow(testOnBorrow);
config.setTestOnReturn(testOnReturn);
config.setBlockWhenExhausted(true); //连接耗尽时是否阻塞,false抛出异常;true阻塞到超时。默认true
pool = new JedisPool(config,redisIp,redisPort,*);
}
static{
initPool();
}
public static Jedis getJedis(){
return pool.getResource();
}
/**
* redis不正常不可用,将其废弃,最新版本直接将此连接销毁jedis.close();
* @param jedis
*/
public static void returnBrokenResource(Jedis jedis){
pool.returnBrokenResource(jedis); //最新版本已废弃
}
public static void returnResource(Jedis jedis){
pool.returnResource(jedis); //最新版本已废弃
}
}
三。redis相关类介绍
(1)JedisPool
JedisPool保证资源在一个可控范围内,并且提供了线程安全,Jedis连接就是资源,JedisPool管理的就是Jedis连接。此类只有三个方法普通方法,大量的构造器,构造器根据不同的连接配置信息,生成对应的资源池。可以很好地重复利用Jedis,减少new的次数,从而提高效率。
(2)JedisPoolConfig
资源池的配置信息,JedisPoolConfig只有一个构造器,大部分配置信息都在继承的类GenericObjectPoolConfig和BaseObjectPoolConfig中。配置最大连接数,最大/小空闲等待数,是否效验生成/归还的连接有效等等。JedisPool其中JedisPoolConfig即是JedisPool构造器所要出入的配置对象,根据JedisPoolConfig配置信息,进行资源池管理。
基本配置如下:
setBlockWhenExhausted(boolean blockWhenExhausted)
当池中的资源耗尽时是否进行阻塞,设置false直接报错,true表示会一直等待,直到有可用资源 setEvictionPolicyClassName(String evictionPolicyClassName)
设置逐出策略,默认策略为
"org.apache.commons.pool2.impl.DefaultEvictionPolicy" setFairness(boolean fairness)
当从池中获取资源或者将资源还回池中时 是否使用java.util.concurrent.locks.ReentrantLock.ReentrantLock 的公平锁机制,默认为false setJmxEnabled
设置是否启用JMX,默认true setJmxNameBase(String jmxNameBase)
设置JMX基础名 setJmxNamePrefix(String jmxNamePrefix)
设置JMX前缀名,默认值pool setLifo(boolean lifo)
设置连接对象是否后进先出,默认true setMaxIdle(int maxIdle)
设置最大空闲连接数,默认为8 setMaxTotal(int maxTotal)
设置最大连接数,默认18个 setMaxWaitMillis(long maxWaitMillis)
获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认- setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis)
设置连接最小的逐出间隔时间,默认1800000毫秒 setMinIdle(int minIdle)
设置无连接时池中最小的连接个数,默认连接0 setNumTestsPerEvictionRun(int numTestsPerEvictionRun)
每次逐出检查时,逐出连接的个数 setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断 setTestOnBorrow(boolean testOnBorrow)
从池中获取连接时是否测试连接的有效性,默认false setTestOnCreate(boolean testOnCreate)
在连接对象创建时测试连接对象的有效性,默认false setTestOnReturn(boolean testOnReturn)
在连接对象返回时,是否测试对象的有效性,默认false setTestWhileIdle(boolean testWhileIdle)
在连接池空闲时是否测试连接对象的有效性,默认false setTimeBetweenEvictionRunsMillis(
long timeBetweenEvictionRunsMillis)
设置连接对象有效性扫描间隔,设置为-,则不运行逐出线程
(3)Jedis
Jedis是Redis官方推荐的Java连接开发工具。要在Java开发中使用好Redis中间件。Jedis有大量的方法,都是进行redis数据库的crud操作。Jedis是有JedisPool连接池创建的。
import com.mmall.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis; /**
* 封装单机redis常用API方法
*/
@Slf4j
public class RedisPoolUtil { /**
* 设置对应key的有效期
* @param key
* @param exTime 有效期,单位秒
* @return
*/
public static Long expire(String key, int exTime){
Jedis jedis = null;
Long result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.expire(key,exTime);
}catch (Exception e){
log.error("set key:{} exTime:{} value:{} error",key,exTime,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 添加,存在有效期exTime
* @param key 键
* @param value 值
* @param exTime 有效期,单位秒
* @return
*/
public static String setEx(String key, String value, int exTime){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.setex(key,exTime,value);
}catch (Exception e){
log.error("set key:{} exTime:{} value:{} error",key,exTime,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 添加
* @param key
* @param value
* @return
*/
public static String set(String key, String value){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.set(key,value);
}catch (Exception e){
log.error("set key:{} value:{} error",key,value,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* string 获取
* @param key
* @return
*/
public static String get(String key){
Jedis jedis = null;
String result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.get(key);
}catch (Exception e){
log.error("get key:{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
} /**
* stirng 删除
* @param key
* @return
*/
public static Long del(String key){
Jedis jedis = null;
Long result = null;
try{
jedis = RedisPool.getJedis();
result = jedis.del(key);
}catch (Exception e){
log.error("get key:{} error",key,e);
RedisPool.returnBrokenResource(jedis);
return result;
}
RedisPool.returnResource(jedis);
return result;
}
}
redis单机连接池的更多相关文章
- redis运用连接池报错解决
redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...
- nodejs + redis/mysql 连接池问题
nodejs + redis/mysql 连接池问题 需不需要连接池 连接池的作用主要是较少每次临时建立连接所带来的开销.初步一看,nodejs运行单线程上,它不能同时使用多个连接,乍一看是不需要连接 ...
- Redis Java连接池调研
Redis Java连接池调研 线上服务,由于压力大报错RedisTimeOut,但是需要定位到底问题出现在哪里? 查看Redis慢日志,slowlog get 发现耗时最大的也是11000us也就是 ...
- python redis之连接池的原理
python redis之连接池的原理 转载地址 什么是连接池 通常情况下, 当我们需要做redis操作时, 会创建一个连接, 并基于这个连接进行redis操作, 操作完成后, 释放连接, 一般情况下 ...
- Redis 配置连接池,redisTemplate 操作多个db数据库,切换多个db,解决JedisConnectionFactory的设置连接方法过时问题。(转)
环境 springmvc jdk1.8 maven redis.properties配置文件 #redis setting redis.host=localhost redis.port=6379 r ...
- Redis缓存连接池管理
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.util.Assert;import ...
- java客户端Jedis操作Redis Sentinel 连接池
pom配置: <dependency> <groupId>org.springframework.data</groupId> <artifactId> ...
- redis mysql 连接池 之 golang 实现
1 mysql 连接池代码 package lib import ( "database/sql" "fmt" "strconv" &quo ...
- python操作Redis安装、支持存储类型、普通连接、连接池
一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...
随机推荐
- cdh版hbase构建Phoenix 遇到的坑
Phoenix 构建cdh版hbase遇到的坑 1. 安装phoenix 下载:在github上下载对应版本https://github.com/apache/phoenix 解压:略 编译: 修改根 ...
- Python学习日记(八)—— 模块一(sys、os、hashlib、random、time、RE)
模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要多个函数才 ...
- 为win10下的linux子系统终端添加powerline
一切按照上一篇完成,如果成功了,你厉害了我的哥,如果不成功,win10安装powerline字体才可以,才可以,才可以 sudo apt install build-essential cmake g ...
- python 进程池和任务量变化测试
今天闲,测试了下concurrent.futures 模块中的ThreadPoolExecutor,ProcessPoolExecutor. 对开不同的数量的进程池和任务量时,所耗时间. from c ...
- 你的windows许可证即将过期
0xC004F025拒绝访问:所请求的操作需要提升特权:解决方法:1.打开我的电脑找到windows System32目录下的cmd 2.对windows System ...
- debian、ubuntu安装metasploit通用方法
网上有很多方法让去github上下载安装,这方法的确可以但是特别慢,更新也特别慢,这里写下比较快的方法 1.添加kali源 vim /etc/apt/sources.list 在原有源的基础上添加国内 ...
- TNetHttpClient的用法
TNetHttpClient的用法 TNetHttpClient是DELPHI XE8新增加的控件. 在之前,我们一般都是使用IDHTTP控件,但在安卓.IOS等非WINDOWS平台,IDHTTP访问 ...
- idea 使用maven 下载源码包
方式1:全量下载源码包 方式二:下载单个源码包 随便找个源码可以看到文件上有download (标识下载源码包) choose sources表示选择那个版本的源码包
- 常见浏览器userAgent请求头信息
if (browser.versions.mobile) {//判断是否是移动设备打开.browser代码在下面 var ua = navigator.userAgent.toLowerCase(); ...
- python实现并发服务器实现方式(多线程/多进程/select/epoll)
python实现并发服务器实现方式(多线程/多进程/select/epoll) 并发服务器开发 并发服务器开发,使得一个服务器可以近乎同一时刻为多个客户端提供服务.实现并发的方式有多种,下面以多进 ...