Store User Information With Redis Or Mongodb
I need to store some user and document state information in a json-like object. For example: { 'name': 'Henry', 'company': 'Disney', 'is_recommended': true, 'plan_t
Solution 1:
You can build a secondary index for the company field with a SET
or LIST
:
SADD company:Disney userid1
SADD company:Disney userid2
SADD company:OtherCompany userid3
When you need to update the data, do the following steps:
- Search the company index to get user ids:
SMEMBERS company:Disney
- Search the user index to get the user attribute: for each user do:
GET userid
- Update the attribute
- Update the user index: for each user do:
SET userid new-attributes
This the built-in way to achieve the goal, it needs more work, and a little complex.
However, as @Not_a_Golfer mentioned in the comment, Redis has a module called RediSearch to do the work for you. If you are playing with Redis 4.0 or above, you can try it.
Post a Comment for "Store User Information With Redis Or Mongodb"