无法使用 redis 模板进行扫描 [英] Not able Scan using redis template

查看:47
本文介绍了无法使用 redis 模板进行扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SCAN http://redis.io/commands/scan 进行迭代redis 中存在的所有键.但是spring提供的Redis模板没有任何scan()方法.上面的使用有什么技巧吗?

I am trying to use SCAN http://redis.io/commands/scan to iterate over all the keys present in redis. But the Redis template provided by spring do not have any scan() method. Is there any trick to use the above?

谢谢

推荐答案

您可以使用 RedisOperations 上的 RedisCallback 来实现.

You can use a RedisCallback on RedisOperations to do so.

redisTemplate.execute(new RedisCallback<Iterable<byte[]>>() {

  @Override
  public Iterable<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {

    List<byte[]> binaryKeys = new ArrayList<byte[]>();

    Cursor<byte[]> cursor = connection.scan(ScanOptions.NONE);
    while (cursor.hasNext()) {
      binaryKeys.add(cursor.next());
    }

    try {
      cursor.close();
    } catch (IOException e) {
      // do something meaningful
    }

    return binaryKeys;
  }
});

这篇关于无法使用 redis 模板进行扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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