Reputation: 1588
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:
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 itWhat else can I do? Or what would you do?
Upvotes: 0
Views: 754
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