栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

redis池--JedisPool

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

redis池--JedisPool

背景

我们基于jedis访问redis时,每次都要获取连接,然后每次都要创建jedis ,释放连接会带来很大的性能开销,我们使用连接池,可以提高性能。

使用步骤 简单的demo演示
public class jedisPooltTest {
    @Test
    public void testJedisPool(){
        String host = "192.168.126.129";
        int port = 6379;
        //定义连接 池配置
       JedisPoolConfig confid = new JedisPoolConfig();
       confid.setMaxTotal(16);//最大连接数,默认8
       confid.setMaxIdle(60);//最大空闲时间,连接后不用,超过时间自动释放。
        //构建jedis连接池
        JedisPool jedisPool = new JedisPool(confid,host,port);
        //从池中获取连接
        Jedis resource = jedisPool.getResource();
        //执行redis操作
        resource.set("pool", "jedis");
        String pool = resource.get("pool");
        System.out.println(pool);
        //释放资源
        resource.close();//(不是关,而是将连接还回池中)
       // jedisPool.close();//关闭池,服务停止时关

    }

使用连接池到具体的业务中 第一步:建立redis数据源,想到与数据库原

public class JedisDateSource {
    private static final JedisPool jedisPool;
    private static final String host = "192.168.126.129";
    private static final int port = 6379;
    static {

        //定义连接 池配置
        JedisPoolConfig confid = new JedisPoolConfig();
        confid.setMaxTotal(16);//最大连接数,默认8
        confid.setMaxIdle(60);//最大空闲时间,连接后不用,超过时间自动释放。
        jedisPool = new JedisPool(confid,host,port);
    }
    //获取连接
    public static Jedis getConnection(){
        return jedisPool.getResource();
    }
  
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/315427.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号