A quick review of Redis’s hash and string structures.

string

We can serialize the user information structure into a string using JSON, and then cache the serialized string in Redis.

String data structure

Because the string of Redis is dynamic and can be modified, its internal structure is similar to That of Java’s ArrayList, and it reduces the frequent allocation of memory by pre-allocating redundant space. As shown in the figure above, the actual space allocated internally for the current string, Capacity, is generally higher than the actual string length len.

Suppose the structure we want to store is:

{
    "name": "xiaowang",
    "age": "35"
}
Copy the code

If you change the name of the user information to Xiaoli and save it to Redis, the redis does not need to reallocate space.

And when we read and store data, we only need to do Json serialization and deserialization, which is convenient.

hash

Redis hash is equivalent to Java HashMap, and its internal structure is the same as HashMap, that is, array + linked list structure. They just rehash it differently,

Hash data structure

String is suitable for storing user information, and the hash structure can store user information separately for each field. Therefore, you can obtain partial field information during query to save network traffic. The hash value of Redis can only be a string. The hash value of Redis can only be a string.

{
    "name": "xiaowang",
    "age": 25,
    "clothes": {
        "shirt": "gray",
        "pants": "read"
    }
}
Copy the code

How to store the “clothes” property then becomes a question of whether to use string or hash.

Since both data structures can store structure information. Which is more appropriate? Someone on StackOverflow asked the same question and let’s take a look at the discussion.

Hash or string

StackOverflow Redis Strings vs Redis Hashes to represent JSON: Efficiency?

The user has the same question, because the length of the value is uncertain, so he does not know whether string or hash storage is more efficient.

Here are the top two answers.

Suitable for string storage

  • You need to access a large number of fields at a time

  • Some key values are stored differently and cannot be stored as strings

Suitable for hash storage

  • In most cases, only a few fields need to be accessed

  • You always know which fields are available in case you don’t get the data you want when you use MGET

It is also strongly recommended to refer to the official memory optimization article: redis. IO /topics/ Memo… , HERE I also give the article address of the Chinese website: www.redis.cn/topics/memo… .

In the official documentation, the author strongly recommends using hash to store data:

Use hashes whenever possible

Small hash tables use very little memory, so you should try to abstract your data model into a hash table as much as possible. For example, if you have a user object in your Web system, do not set a separate key for the user’s name, last name, email address, and password. Instead, store all of the user’s information in a hash table.

Refer to the link

  1. StackOverflow:stackoverflow.com/questions/1…
  2. Redis. IO /topics/ Memo…