使用redis或mongodb存储用户信息 [英] Store user information with redis or mongodb
问题描述
我需要在一个类似 json 的对象中存储一些用户和文档状态信息.例如:
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_type" "free",
etc.
}
当用户登录或更改任何用户信息时,此信息从数据库中获取并存储在会话中的内存中.
This information is fetched from the database and stored in memory in the session when the user logs in or changes any user information.
我对 redis 有一些经验,我发现自己对使用它很满意,但我想知道是否可以在 redis 中完成上述操作,而不会跳太多圈.例如,以下是我需要运行的一些查询:
I have some experience with redis and I find myself comfortable with using that, but I was wondering if the above could be done in redis without jumping through too many hoops. For example, here are some queries I would need to run:
update items set plan_type="Paid" where company = "Disney";
您认为在 redis 中可以执行上述操作,还是应该尝试使用其他东西(我认为是 mongodb)来完成上述操作?
Do you think doing the above would be possible in redis, or should I try using something else (my thought was mongodb) to accomplish the above?
99% 的使用是读取数据,但 1% 是批量更新数据,并且需要即时完成.
99% of the usage would be reading data, however 1% would be updating data in bulk fashion, and it'd need to be done instantaneously.
六年前有人问过类似的问题 -- 存储数千个中型文档的最有效的面向文档的数据库引擎是什么? -- 但我相信从那时起 redis 和 mongodb 都发生了很大的变化......
A similar question was asked six years ago -- What's the most efficient document-oriented database engine to store thousands of medium sized documents? -- but I'm sure much has changed in both redis and mongodb since then...
推荐答案
您可以使用 SET
或 LIST<为 company 字段构建二级索引/代码>:
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:
- 搜索公司索引以获取用户 ID:
SMEMBERS company:Disney
- 搜索用户索引以获取用户属性:对于每个用户执行:
GET userid
- 更新属性
- 更新用户索引:为每个用户做:
SET userid new-attributes
这是实现目标的内置方式,需要更多的工作,而且有点复杂.
This the built-in way to achieve the goal, it needs more work, and a little complex.
然而,正如@Not_a_Golfer 在评论中提到的,Redis 有一个名为 RediSearch 的模块来做为你工作.如果你在玩Redis 4.0或以上,可以试试.
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.
这篇关于使用redis或mongodb存储用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!