首页 > 编程知识 正文

redis工具类封装,jedis连接redis

时间:2023-05-05 10:52:01 阅读:34198 作者:4033

import java.util.ResourceBundle; import redis.clients.jedis.jedis; import redis.clients.jedis.Jedi spool; import redis.clients.jedis.jedis poolconfig; public class RedisUtil {//端口、地址、用户私有身份验证资源绑定lemyresourcebundlemyresourcesus=资源绑定. get bundle (' db ' ); //my properties _ en.propertiesprivatestaticstringip=myresourcesus.getstring (' redis.hostname ); privatestaticintport=integer.parseint (myresourcesus.getstring (' redis.port ' ); privatestaticstringauth=myresourcesus.getstring (redis.password ); //可用连接实例的最大数量,默认值为8; -1表示不限制私有静态int max _ active=20。//控制一个pool中最多有多少个处于idle (空闲)状态的jedis实例,默认值也是8。 私有静态int max _ idle=8; //等待可用连接的最长时间。 毫秒单位。 默认值为-1,表示不超时。 超过等待时间时,返回JedisConnectionException; 私有静态int max _ wait=10000; 私密静态int time out=10000; borrow一个jedis实例时,是否提前执行validate操作; 如果为true,则所有得到的jedis实例都可用; 私密性staticbooleantest _ on _ borrow=true; 私密性统计信息=空值;/* * *发现redis连接池*/static {try {//是类似于2.8.2的jedis//jar包的较高版本,在使用中使用了JedisPoolConfig。 没有setMaxActive和setMaxWait属性//max active==max total//max wait==maxwaitmillisjedispoolconfigconfig=newjedispool config.setmaxtotal(max_active; config.setmaxidle(max_idle; config.setmaxwaitmillis (max _ wait ); config.settestonborrow (test _ on _ borrow; Jedispool=newJedispool(config,IP,PORT,TIMEOUT,AUTH ); }catch(exceptione ) {e.printStackTrace ); }/****Jedis实例* * @ return */publicsynchronizedstaticjedisgetjedis (() try ) if(Jedispool!=null (jedis resource=Jedi spool.get resource ); 返回资源; } else {return null; }catch(exceptione ) {e.printStackTrace ); 返回空值; }/***Jedis资源* * @ param jedis */publicstaticvoidreturnresource (finaljedisjedis ) ) try )/system.out.println/if(Jedis.isconnected () ) {jedis.quit ); jedis.disconnect (; jedis.close (; }catch(exceptione ) {System.out.println )“退出失败”e.printStackTrace (; }/* * publicstaticvoidmain (string [ ] args ) { Jedis redis=RedisUtil.getJedis ); * RedisUtil.getJedis ().set ) ' billrouting ','8' ); * redis util.return资源(redis util.get jedis () ); * system.out.println (redis util.get jedis ().get ) ' billrouting ' ); ()/) }

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