Jedis连接池

Jedis连接池

1.jedis连接池测试的代码如下:

package com.txw.jedis.test;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
 * jedis的测试类
 * @author Adair
 */
@SuppressWarnings("all")       // 注解警告信息
public class JedisTest {
    /**
     * jedis连接池使用
     */
    @Test
    public void testJedisPool(){
        //创建一个配置对象
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(10);
        // 创建Jedis连接池对象
        JedisPool jedisPool = new JedisPool(config,"localhost",6379);
        // 获取连接
        Jedis jedis = jedisPool.getResource();
        // 使用
        jedis.set("hehe","heihei");
       // 关闭 归还到连接池中
        jedis.close();;
    }
}    

运行结果如图所示:在这里插入图片描述
在Redis客户端运行结果如图所示:
在这里插入图片描述
jedis详细配置的代码如下:

#最大活动对象数     
redis.pool.maxTotal=1000    
#最大能够保持idel状态的对象数      
redis.pool.maxIdle=100  
#最小能够保持idel状态的对象数   
redis.pool.minIdle=50    
#当池内没有返回对象时,最大等待时间    
redis.pool.maxWaitMillis=10000    
#当调用borrow Object方法时,是否进行有效性检查    
redis.pool.testOnBorrow=true    
#当调用return Object方法时,是否进行有效性检查    
redis.pool.testOnReturn=true  
#“空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.  
redis.pool.timeBetweenEvictionRunsMillis=30000  
#向调用者输出“链接”对象时,是否检测它的空闲超时;  
redis.pool.testWhileIdle=true  
# 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.  
redis.pool.numTestsPerEvictionRun=50  
#redis服务器的IP    
redis.ip=xxxxxx  
#redis服务器的Port    
redis1.port=6379  

2.编写JedisPool工具类的代码如下:

package com.txw.jedis.util;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * JedisPool工具类
 *     加载配置文件,配置连接池的参数
 *     提供获取连接的方法
 * @author Adair     
 */
@SuppressWarnings("all")        // 注解警告信息
 public class JedisPoolUtils {
     // 声明JedisPool业务对象
    private static JedisPool jedisPool;
    static{
        // 读取配置文件
        InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
        // 创建Properties对象
        Properties pro = new Properties();
        // 关联文件
        try {
            pro.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取数据,设置到JedisPoolConfig中
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
        config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
        // 初始化JedisPool
        jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
    }
    /**
     * 获取连接方法
     */
    public static Jedis getJedis(){
        return jedisPool.getResource();
    }
}

3.在resources目录创建jedis.properties的代码如下:

host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10

4.测试类代码如下:

package com.txw.jedis.test;

import com.txw.jedis.util.JedisPoolUtils;
import org.junit.Test;
import redis.clients.jedis.Jedis;
/**
 * jedis的测试类
 * @author Adair
 */
@SuppressWarnings("all")       // 注解警告信息
public class JedisTest {
    /**
     * jedis连接池工具类使用
     */
    @Test
    public void testJedisPoolUtils(){
        // 通过连接池工具类获取
        Jedis jedis = JedisPoolUtils.getJedis();
        // 使用
        jedis.set("hello","world");
        // 关闭 归还到连接池中
        jedis.close();;
    }
}

运行结果如图所示:在这里插入图片描述
在Redis客户端运行如图所示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值