Redis storing and getting multiple records

Viewed 55

I'm quite new to redis and have a task at hand to optimize redis operations for a dashboard of a non-profit.

The scenario:

  • We have multiple cities (London, Rome,...)
  • We have 1000s of users in each city

We have a need to load users from a particular city, and at the moment it is stored by city ID. As you can imagine, this results in a massive document that needs to be re-cached on every small change to the user.

I want to change this methodology so users are stored in Redis in the following format: [cityID][userID]. So if I need to pull all users from london, I can just call IDFORLONDON?

Would this be the correct way to approach it? Is there a way to load only 10 users from IDFORLONDON? (for pagination). Or is my option to load all and then slice?

Thank you!

1 Answers

I'm gonna recommend you go with RedisJSON if you can. RedisJSON is a module that you can add to Redis to extend its capabilities—in this case adding JSON documents and a whole host of commands to read, write, and manipulate them.

From the Redis command-line it looks like this:

127.0.0.1:6379> JSON.SET london . '{ "users": [ "alice", "bob", "frank" ]}'
OK
127.0.0.1:6379> JSON.GET london .users
"[\"alice\",\"bob\",\"frank\"]"
127.0.0.1:6379> JSON.ARRAPPEND london .users '"charlie"'
(integer) 4
127.0.0.1:6379> JSON.GET london .users
"[\"alice\",\"bob\",\"frank\",\"charlie\"]"
Related