skip to Main Content

How can i get top of N keys from redisson client?

I found next signature in getKeysByPattern() method:

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

But looks like count – is keys loaded per request to Redis.

How can i load top N keys from Redis via redisson client?

2

Answers


  1. Chosen as BEST ANSWER

    getKeysByPattern() is not valid for such case, right.

    It's better to use lua scripts here:

    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)
    

  2. You need to use RKeys.getKeysWithLimit(String pattern, int limit) method for this case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search