cachingredispaginationset

How to do paging on redis's sets data structure?


I want to use redis's sets data structure to store user's followers list, How I can do paging to fetch records from list based on page size?


Solution

  • On top of what @Didier Spezia wrote, I'd also try to store the followers in a sorted set.

    If you want them by name, store them all with score of 0, and then use ZRANGEBYLEX to page them lexically.

    If you want by the order that they were added as friends, use the timestamp of addition as the score, and use ZRANGE to page them by time.

    [EDIT] oh, another option to look at if memory concerns you: if the ids are constant length integers, you can store them as binary values in an array using a string key, and page it with ranges. It will be super fast and will have virtually 0 memory overhead. Have a look at the BITFIELD command. It might be a very good use case for it, although it doesn't cover deletion and lookup, so if those are a concern it's not a good option.