Search code examples
redislettuce

What is the right way to read a zset in a redis cluster with an open-ended max bound with lettuce?


I have used ZADD command to insert a bunch of IDs with their corresponding scores into a redis instance. The score is basically a timestamp at which the ZADD is called.

Now I want to retrieve a list of IDs whose score is bigger than the timestamp of the moment five minutes ago.

The client is written in java and I am using lettuce as the redis client library.

I have a few questions:

  1. Here is a link to the documentation of zrangebyscore on redis website (https://redis.io/commands/zrangebyscore). However on the lettuce website the counterpart is marked as 'Deprecated'. Is it a discrepancy of documentations, or lettuce has retired the support of this API?

enter image description here

  1. I want to be able to retrieve a list of ID whose score is bigger than a certain number N, but I do not care about the upper-end.

In lettuce's documentation this API zrange seems to be ideal for my purpose. However what sopt I can use to express that I do not care about the upper-bound? The documentation is not clear about this.

enter image description here


Solution

  • The Redis zrange command is a zero index based command. This means the indexes start from 0 and increments as you add new elements. What's helpful here is you can retrieve the last index by specifying negative index -1, second from last by specifying -2 and so on. See more details about zrange on the redis website here.

    To retrieve the entire range, you can run

    zrange keyname 0 -1
    

    Note that '0' can be replaced by any index which means the above command would fetch a value starting at that index location. Therefore, this cannot be directly used to get all values higher than a specific score.

    To retrieve from a specific score N, use

    zrangebyscore keyname N +inf
    

    Here is the right Lettuce API for zrangebyscore available since Lettuce 4.3