mouchin777
mouchin777

Reputation: 1588

Redis, trying to figure out best way to store data in order to display it paginated

Im having the following issue.

I currently have a redis database containing keys, a lot of them, >100k. I want to paginate it server side.

My research have showed me the following:

  1. My current storage is based on key -> value . You can't paginate key/value per my research without adding too much complexity, so I would discard this option, altho it would be the easiest way since I already have all the data stored that way. Otherwise if its not possible, i would just want to get keys * , and I can get all the keys, and then make queries to read each one separately. But i dont know if its the best way to do it
  2. Migrating the key-value storage to Sorted Sets and use ZRANGE. But... will that data be stable? I mean anything can happen, and some new keys get added and then it wouldn't make sense, and it would have missing lines when switching pages for example. Am I right? Anyway in this case I was thinking about getting the whole length with ZCOUNT and then with ZRANGE retrieving paginated data.

What else can I do? Or what would you do?

Upvotes: 0

Views: 754

Answers (1)

for_stack
for_stack

Reputation: 22936

You can save your data as key-value pairs, and use a sorted to record the order of each key inserted, i.e. use a monotonically increasing counter as the score of the sorted set. So that newly added keys will always be put at the end. Wrap the logic into a Lua script:

local key = KEYS[1]
local val = ARGV[1]

redis.call("SET", key, val)

local score = redis.call("INCR", "score")

redis.call("ZADD", "pagination", "NX", score, key)

Then you can use ZRANGE to do pagination: ZRANGE pagination 0 10

Upvotes: 2

Related Questions