redisson客户端:检索前n个密钥

bfhwhh0e  于 2021-06-08  发布在  Redis
关注(0)|答案(2)|浏览(638)

如何从redisson客户端获取n个密钥中的最上面一个?
我发现了下一个签名 getKeysByPattern() 方法:

Iterable<String> getKeysByPattern(String pattern, int count);

但看起来像 count -是每个redis请求加载的密钥。
如何通过redisson客户端从redis加载前n个密钥?

vc6uscn9

vc6uscn91#

getKeysByPattern() 在这种情况下是无效的,对吧。
最好在这里使用lua脚本:

val luaScript = "return {redis.call('SCAN',ARGV[1],'MATCH',ARGV[2],'COUNT',ARGV[3])}"
var cursor = 0
do {
  val data = redissonClient
                .getScript(StringCodec.INSTANCE)
                .eval(RScript.Mode.READ_ONLY,
                        luaScript,
                        RScript.ReturnType.MAPVALUELIST,
                        listOf(),
                        cursor,
                        "some-pattern",
                        batchSize)

  val redistListData = (data[0][1] as List<String>)
  cursor = data[0][0].toInt()
} while (cursor != 0)
des4xlb0

des4xlb02#

你需要使用 RKeys.getKeysWithLimit(String pattern, int limit) 此案例的方法。

相关问题