node.js redis 性能,将json对象存储为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5729891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:03:18  来源:igfitidea点击:

redis performance, store json object as a string

node.jsredis

提问by Luc

I need to save a User model, something like:

我需要保存一个用户模型,例如:

{ "nickname": "alan",
  "email": ...,
  "password":...,
  ...} // and a couple of other fields

Today, I use a Set: users
In this Set, I have a member like user:alan
In this member I have the hash above

今天,我使用了一个 Set: users
在这个 Set 中,我有一个像 user:alan
这样的成员在这个成员中我有上面的哈希

This is working fine but I was just wondering if instead of the above approach that could make sense to use the following one:

这工作正常,但我只是想知道是否可以使用以下方法代替上述方法:

Still use users Set (to easily get the users (members) list)
In this set only use a key / value storage like:

仍然使用 users Set(轻松获取用户(成员)列表)
在此集合中仅使用键/值存储,例如:

key: alan value : the stringify version of the above user hash

键:alan 值:上述用户哈希的字符串化版本

Retrieving a record would then be easier (I will then have to Parse it with JSON).

检索记录会更容易(然后我将不得不用 JSON 解析它)。

I'm very new to redis and I am not sure what could be the best. What do you think ?

我对 redis 很陌生,我不确定什么是最好的。你怎么认为 ?

采纳答案by yojimbo87

You can use Redis hashesdata structure to store your JSON object fields and values. For example your "users" set can still be used as a list which stores all users and your individual JSON object can be stored into hash like this:

您可以使用 Redis哈希数据结构来存储您的 JSON 对象字段和值。例如,您的“用户”集仍然可以用作存储所有用户的列表,并且您的个人 JSON 对象可以像这样存储到哈希中:

db.hmset("user:id", JSON.stringify(jsonObj));

Now you can get by key all users or only specific one (from which you get/set only specified fields/values). Also thesetwoquestions are probably related to your scenario.

现在,您可以通过键获取所有用户或仅特定用户(从中获取/设置仅指定的字段/值)。此外,这些2点的问题都可能与您的方案。

EDIT: (sorry I didn't realize that we talked about this earlier)

编辑:(对不起,我没有意识到我们之前讨论过这个)

Retrieving a record would then be easier (I will then have to Parse it with JSON).

检索记录会更容易(然后我将不得不用 JSON 解析它)。

This is true, but with hash data structure you can get/set only the field/value which you need to work with. Retrieving entire JSON object can result in decrease of performance (depends on how often you do it) if you only want to change part of the object (other thing is that you will need to stringify/parse the object everytime).

这是真的,但是使用哈希数据结构,您只能获取/设置您需要使用的字段/值。如果您只想更改对象的一部分(另一件事是您每次都需要对对象进行字符串化/解析),则检索整个 JSON 对象可能会导致性能下降(取决于您这样做的频率)。

回答by David

One additional merit for JSON over hashes is maintaining type. 123.3becomes the string "123.3"and depending on library Null/Nonecan accidentally be casted to "null".

JSON 优于散列的另一个优点是保持类型。 123.3成为字符串,"123.3"并且根据库Null/None可能会意外地强制转换为"null".

Both are a bit tedious as that will require writing a transformer for extracting the strings and converting them back to their expected types.

两者都有些乏味,因为这需要编写一个转换器来提取字符串并将它们转换回预期的类型。

For space/memory consumption considerations, I've started leaning towards storing just the values as a JSON list ["my_type_version", 123.5, null , ... ]so I didn't have overhead of N * ( sum(len(concat(JSON key names)))which in my case was +60% of Redis's used memory footprint.

出于空间/内存消耗的考虑,我开始倾向于仅将值存储为 JSON 列表,["my_type_version", 123.5, null , ... ]因此我没有开销N * ( sum(len(concat(JSON key names))),在我的情况下,Redis 已用内存占用量的 60%。

回答by mzalazar

bear in mind: Hashes cannot store nested objects, JSON can do it.

请记住:哈希不能存储嵌套对象,JSON 可以。

回答by BMiner

Truthfully, either way works fine. The way you store it is a design decision you will need to make. It depends on how you want to retrieve the user information, etc.

说实话,无论哪种方式都可以正常工作。您存储它的方式是您需要做出的设计决定。这取决于您希望如何检索用户信息等。

In terms of performance, storing the JSON encoded version of the user object will use less memory and take less time for storage/retrieval. That is, JSON parsing is probably faster than retrieving each field from Redis. And, even if not, it is probably more memory efficient. The difference in performance is probably minimal anyway.

在性能方面,存储用户对象的 JSON 编码版本将使用更少的内存并花费更少的时间进行存储/检索。也就是说,JSON 解析可能比从 Redis 检索每个字段更快。而且,即使没有,它也可能具有更高的内存效率。无论如何,性能差异可能很小。