首页 > 编程知识 正文

redis连接池参数,redis客户端连接池配置

时间:2023-05-06 12:33:53 阅读:33125 作者:1935

1新的依赖关系

4.0.0

com.gc

自述文件

0.0.1-快照

redis.clients

杰迪斯

2.9.0

junit

junit

4.12

测试

org.apache.commons

公用池2

2.4.2

因为您使用的是jedis2.9,所以选择了使用jedisPool构建jedisPool的第三种方法

公共Jedi spool (

this(protocol.default_host,Protocol.DEFAULT_PORT );

}

publicjedispool (finalgenericobjectpoolconfigpoolconfig,final String host ) {

this(poolconfig,host,Protocol.DEFAULT_PORT,Protocol.DEFAULT_TIMEOUT,null,

Protocol.DEFAULT_DATABASE,null;

}

publicjedispool (字符串主机,int端口) {

this (newgenericobjectpoolconfig (,主机,端口,协议. default _ time out,null,

Protocol.DEFAULT_DATABASE,null;

}

GenericObjectPoolConfig具有默认的config信息,可以使用此方法初始化jedisPool连接池

包就绪演示;

import org.junit.Test;

import redis.clients.jedis.jedis;

import redis.clients.jedis.Jedi spool;

//*

* redis连接池

* @Description: TODO (用一句话描述此文件将做什么) ) ) ) ) ) ) ) ) )。

* @author gc

* @date 2018年10月2日

*

*/

公共类注册池{

公共jedis get jedis

jedispooljedispool=newjedispool (' 192.168.34.3 ',6379 );

Jedis jedis=null;

try{

jedis=jedisPool.getResource (;

jedis.auth (根);

}catch(exceptione ) {

e .打印堆栈跟踪(;

}finally {

if(Jedispool!=空) {

jedisPool.close (;

}

}

返回杰迪;

}

@Test

公共语音测试jedis

Jedis jedis=getJedis (;

字符串用户名称=jedis.get (username );

系统. out.println (username;

jedis.close (;

}

}

完成连接池的首次创建,并在版本2.9中继承GenericObjectPoolConfig

package redis.clients.jedis;

importorg.Apache.com mons.pool2. impl.genericobjectpoolconfig;

publicclassjedispoolconfigextendsgenericobjectpoolconfig {

公共jedis poolconfig (

//defaultstomakeyourlifewithconnectionpooleasier : )

settestwhileidle(true;

setminevictableidletimemillis (60000;

sttimebetweenevictionrunsmillis (30000;

setnumtestsperevictionrun(-1;

}

}

因此,使用默认连接池配置

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。