Luc
Luc

Reputation: 17072

keys or list for my redis datamodel?

I'm building the data model of my app and basically, for a given user, I'd like to keep a list of his/her friends and the status of each of them (if they accepted the request to be friends or if they did not yet)

I end up with several keys: (one for each friend of tom)

friends:tom:status:jessica => joined
friends:tom:status:stephan => joined
friends:tom:status:hubert => pending
friends:tom:status:peter => declined

Is that the best way to handle that or should a list be used in some other way ?

Upvotes: 0

Views: 144

Answers (2)

Dmytro Yashkir
Dmytro Yashkir

Reputation: 1225

You could use an ordered set for this.

Have each status have a score associated with it, joined 1, pending 2, declined 3

zadd user1_friends 1 userid 1 userid 2 userid

then you can easily retrieve all users by category

zscore user1_friends 1

or you could split into 3 separate sets

sadd user1_joined userid1
sadd user1_pending userid3

Depending on what you want to do either will work

Upvotes: 2

yojimbo87
yojimbo87

Reputation: 68285

You can try to use for example hash structure where hash key would be friends:tom:status, field would represent friend name/ID and value would represent his status. Hash structure is more memory efficient than dedicated keys in general.

Upvotes: 2

Related Questions