简体   繁体   English

使用Redis或mongodb存储用户信息

[英]Store user information with redis or mongodb

I need to store some user and document state information in a json-like object. 我需要在类似json的对象中存储一些用户和文档状态信息。 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. 当用户登录或更改任何用户信息时,将从数据库中获取此信息并将其存储在会话的内存中。

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. 我对Redis有一定的经验,我对使用它感到很满意,但是我想知道是否可以在Redis中完成以上操作而又不会遇到太多麻烦。 For example, here are some queries I would need to run: 例如,这是我需要运行的一些查询:

update items set plan_type="Paid" where company = "Disney";

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? 您是否认为可以在redis中进行上述操作,还是应该尝试使用其他方法(我认为是mongodb)来完成上述操作?

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. 使用率的99%将用于读取数据,但是1%的将用于批量更新数据,因此需要立即完成。

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... -但是我敢肯定,自那时以来,redis和mongodb都发生了很大变化。

You can build a secondary index for the company field with a SET or LIST : 您可以使用SETLISTcompany字段构建二级索引:

SADD company:Disney userid1
SADD company:Disney userid2
SADD company:OtherCompany userid3

When you need to update the data, do the following steps: 当您需要更新数据时,请执行以下步骤:

  1. Search the company index to get user ids: SMEMBERS company:Disney 搜索公司索引以获取用户ID: SMEMBERS company:Disney
  2. Search the user index to get the user attribute: for each user do: GET userid 搜索用户索引以获取用户属性:对于每个用户,执行以下操作: GET userid
  3. Update the attribute 更新属性
  4. Update the user index: for each user do: SET userid new-attributes 更新用户索引:对每个用户执行以下操作: 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. 但是,正如注释中提到的@Not_a_Golfer一样,Redis有一个名为RediSearch的模块可以为您完成工作。 If you are playing with Redis 4.0 or above, you can try it. 如果您正在使用Redis 4.0或更高版本,则可以尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM