Jedis Pool如何运作? [英] How Jedis Pool works?

查看:256
本文介绍了Jedis Pool如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jedis池来管理与redis服务器的连接。我的示例代码如下:

I'm using jedis pool to manage connections to redis server. An example code of mine as follows:

<!-- language: lang-java -->
public Set<String> getTopArticleList(int start, int end) {
    Set<String> list = null;
    Jedis j = JedisFactory.getInstance().getJedisPool().getResource();
    Pipeline pipe = j.pipelined();
    try {
        // do stuff with redis
        pipe.sync();
    } catch (JedisConnectionException jex) {
        JedisFactory.getInstance().getJedisPool().returnBrokenResource(j);
    } finally {
        JedisFactory.getInstance().getJedisPool().returnResource(j);
    }
    return list;
}

检索jedis池的代码

And code to retrieve jedis pool

<!-- language: lang-java -->
class JedisFactory {
    private static JedisPool jedisPool;
    public JedisFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        jedisPool = new JedisPool(
            poolConfig,
            RedisDBConfig.HOST,
            RedisDBConfig.PORT,
            RedisDBConfig.TIMEOUT,
            RedisDBConfig.PASSWORD
        );
    }

    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public static JedisFactory getInstance() {
        if (instance == null) {
            instance = new JedisFactory();
        }
        return instance;
    }
 }            

问题是在达到有限连接数后,网络不再被访问。我做错了什么?

The problem is that after reaching the number of limited connections, the web cannot be accessed anymore. Am I doing something wrong?

推荐答案

您尚未配置池的 maxTotal 大小,您可以将JedisFactory构造函数更改为:

You haven't configured the maxTotal size of the pool, and the default value is only 8. You could change the JedisFactory constructor to:


    public JedisFactory() {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(128);
        jedisPool = new JedisPool(poolConfig, RedisDBConfig.HOST, RedisDBConfig.PORT, RedisDBConfig.TIMEOUT, RedisDBConfig.PASSWORD);
    }

还要注意 WhenExhaustedAction 的默认值(WHEN_EXHAUSTED_BLOCK),因为它可能不是你想要的行为。

Beware also of the default value of the WhenExhaustedAction (WHEN_EXHAUSTED_BLOCK), as it may not be your desired behavior.

Jedis使用Apache Commons Pool,你可以在这里阅读它的参数: GenericObjectPool

Jedis uses Apache Commons Pool, and you can read about it's parameters here: GenericObjectPool

这篇关于Jedis Pool如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆