redis performance, store json object as a string

前端 未结 4 402
夕颜
夕颜 2021-01-31 04:13

I need to save a User model, something like:

{ \"nickname\": \"alan\",
  \"email\": ...,
  \"password\":...,
  ...} // and a couple of other fields
4条回答
  •  悲哀的现实
    2021-01-31 05:02

    You can use Redis hashes data 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:

    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 these two questions are probably related to your scenario.

    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).

    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).

提交回复
热议问题