首页 > 编程知识 正文

java容器有哪几种,java连接redis集群

时间:2023-05-06 02:21:28 阅读:133141 作者:838

用jedispool操作redis的工具类,通过大量测试和高并发测试。

使用jedispool时一定要注意两点

1 .获取jedisPool和jedis时添加线程同步,确保不创建过多的jedisPool和jedis

用完Jedis实例后,需要将其返回给JedisPool

import org.apache.log4j.Logger;

import redis.clients.jedis.jedis;

import redis.clients.jedis.Jedi spool;

import redis.clients.jedis.jedis poolconfig;

//*

*就绪工具类

* @author caspar

* http://blog.csdn.net/tuposky

*/

公共类就绪util {

protectedstaticloggerlogger=logger.getlogger (redis util.class;

//Redis服务器IP

privatestaticstringaddr _ array=file util.getproperty value ('/properties/redis.properties ',' server ' );

//Redis的端口号

privatestaticintport=file util.getpropertyvalueint ('/properties/redis.properties ',' port ' );

//访问密码

//privatestaticstringauth=file util.getproperty value ('/properties/redis.properties ',' auth ' );

//可用连接实例的最大数量,默认值为8;

代入//-1,表示不限制; 如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted。

privatestaticintmax_active=file util.getpropertyvalueint ('/properties/redis.properties ',' max _ active ' );

//控制一个pool中最多有多少个处于idle (空闲)状态的jedis实例,默认值也是8。

privatestaticintmax_idle=file util.getpropertyvalueint ('/properties/redis.properties ',' max _ idle ' );

//等待可用连接的最长时间。 毫秒单位。 默认值为-1,表示不超时。 超过等待时间时,返回JedisConnectionException;

privatestaticintmax_wait=file util.getpropertyvalueint ('/properties/redis.properties ',' max _ wait ' );

//超时时间

privatestaticinttimeout=file util.getpropertyvalueint ('/properties/redis.properties ',' timeout ' );

borrow一个jedis实例时,是否提前执行validate操作; 如果为true,则所有得到的jedis实例都可用;

privatestaticbooleantest _ on _ borrow=file util.getpropertyvalueboolean ('/properties/redis.properties ',' test ;

私密性统计信息=空值;

//*

* redis的有效期限,以秒为单位

*/

publicfinalstaticintexrp _ hour=60 * 60; //1小时

publicfinalstaticintexrp _ day=60 * 60 * 24; //一天

publicfinalstaticintexrp _ month=60 * 60 * 24 * 30; //一个月

//*

初始化Redis连接池

*/

专用静态语音初始化池

try {

jedispoolconfigconfig=newjedispoolconfig (;

config.setmaxtotal(max_active;

config.setmaxidle(max_idle;

config.setmaxwaitmillis (max _ wait );

config.settestonborrow (test _ on _ borrow;

JEDIspool=newJedispool(config,ADDR_ARRAY.split (',') [0],PORT,TIMEOUT );

}catch(exceptione ) {

logger.error (firstcreatejedispoolerror : ) e;

try{

//如果第一个IP异常,则访问第二个IP

jedispoolconfigconfig=newjedispoolconfig (;

config.setmaxtotal(max_active;

config.setmaxidle(max_idle;

config.setmaxwaitmillis (max _ wait );

config.settestonborrow (test _ on _ borrow;

JEDIspool=newJedispool(config,ADDR_ARRAY.split (',') ) [1],PORT,TIMEOUT;

}catch(exceptionE2) {

logger.error (secondcreatejedispoolerror 3360 ) E2;

}

}

}

//*

*多线程环境下的同步初始化

*/

私有状态同步化语音语音点(

if(Jedispool==null ) {

initialPool (;

}

}

//*

同步获取Jedis实例

* @return Jedis

*/

publicsynchronizedstaticjedisgetjedis (

if(Jedispool==null ) {

poolInit (;

}

Jedis jedis=null;

try {

if(Jedispool!=空) {

jedis=jedisPool.getResource (;

}

}catch(exceptione ) {

logger.error (getjediserror : ) e;

}finally{

返回资源(jedis;

}

返回杰迪;

}

//*

释放jedis资源

* @param jedis

*/

publicstaticvoidreturnresource {

if(Jedis!=空Jedi spool!=空) {

Jedispool.returnresource(Jedis;

}

}

//*

字符串设置

* @param key

* @param value

*/

publicstaticvoidsetstring (字符串密钥,字符串值)。

try {

value=stringutil.isempty(value? ' ' : value;

getJedis ().set )、key、value;

}catch(exceptione ) {

Logger.error(setkeyerror: ) e;

}

}

//*

*设置过期日期

* @param key

* @param seconds以秒为单位

* @param value

*/

publicstaticvoidsetstring (字符串密钥,int seconds,字符串值) {

try {

value=stringutil.isempty(value? ' ' : value;

getJedis ().setex(key,seconds,value );

}catch(exceptione ) {

logger.error (setkeyexerror : ) e;

}

}

//*

获取字符串值

* @param key

* @return value

*/

publicstaticstringgetstring (字符串密钥) {

if (获取jedis (==null |! getJedis ().exists ) ) key ) }

返回空值;

}

返回get jedis ().get ) key;

}

}

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