gpt4 book ai didi

redis - Spring Data Redis - 支持命令 PUBSUB CHANNELS

转载 作者:可可西里 更新时间:2023-11-01 10:58:04 25 4
gpt4 key购买 nike

RedisTemplate 不支持PUBSUB CHANNELS 命令。所以一种方法是执行以下操作

   private JedisPool getJedisPool(){
if (jedisPool == null)
jedisPool = new JedisPool(redisConnectionFactory.getPoolConfig(), redisConnectionFactory.getHostName(), redisConnectionFactory.getPort());
return jedisPool;
}

public Integer getNumChannels() {
Integer count = 0;
try (Jedis jedis = getJedisPool().getResource()) {
List<String> channels = jedis.pubsubChannels("user.*");
count = channels == null ? 0 : channels.size();
} catch (Exception e) {
logger.error("unable to get user count", e);
} finally {
//getJedisPool().close(); //No need for close or returnResource()
}
}

这是建议的方法吗?

最佳答案

这取决于您要去哪里。如果你打算只将它用于你自己的应用程序,那么你可以从 JedisConnectionFactory 获取一个 JedisConnection 并使用底层的 Jedis 实例来调用命令.

JedisConnectionFactory factory = …

// assuming you're using Redis Standalone or Redis Sentinel
RedisConnection connection = factory.getConnection();
try {
if (connection instanceof JedisConnection) {
Jedis jedis = ((JedisConnection) connection).getNativeConnection();
List<String> strings = jedis.pubsubChannels("…");
}
} finally {
connection.close();
}

请注意,这仅适用于 Redis Standalone/Redis Sentinel,但不适用于 Redis Cluster,因为 JedisCluster 不会公开 pubsubChannels

关于redis - Spring Data Redis - 支持命令 PUBSUB CHANNELS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36092174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com