有没有办法让 Jedis 自动将连接池用于命令方法?

问题描述

我在我正在从事的项目中遇到一个

public class RedisClient {
    Logger logger = LoggerFactory.getLogger(RedisClient.class);
    JedisPool pool;
    public RedisClient(String redisHost,int redisPort,String redisPassword) {
        JedisPoolConfig poolConfig = buildPoolConfig();
        try {
            pool = new JedisPool(poolConfig,redisHost,redisPort,10000,redisPassword,true);
        } catch (Exception e) {
            logger.error("There's been an error while Jedis attempted to retrieve a thread from the pool",e);
        }
    }
    public void set(String key,String value) {
        try (Jedis jedis = pool.getResource()) {
            jedis.set(key,value);
        }
    }
   // ... a few more command methods wrapped with try (Jedis jedis = pool.getResource())
   // like get,expire,etc.

虽然这无论如何都不是一个糟糕的方法(在我看来这是非常标准的适配器模式),但我想知道 Jedis 是否会自动处理这个问题(来自 Python,我希望它可能隐藏了这个细节) la redis,或者有另一种方法可以将现有客户端配置为“为每个命令使用一个连接池”。我看到 jedis.Jedis一个 setDataSource 方法,它接受 {{1 }},但我无法确定它的实际作用以及它是否能帮助我回答我的问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)