【问题标题】:Spring Data Redis - Support for command PUBSUB CHANNELSSpring Data Redis - 支持命令 PUBSUB CHANNELS
【发布时间】:2016-07-05 16:01:35
【问题描述】:

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()
        }
    }

这是建议的方法吗?

【问题讨论】:

    标签: redis spring-data spring-data-redis


    【解决方案1】:

    这取决于你要去哪里。 如果您打算仅将它用于您自己的应用程序,那么您可以从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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 2017-05-06
      • 2021-03-09
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      相关资源
      最近更新 更多