Home >Database >Redis >When does redis use hash type?

When does redis use hash type?

尚
Original
2019-07-04 16:16:093710browse

When does redis use hash type?

The Hash type is a field and value mapping table of the String type, or a String collection. It is particularly suitable for storing objects. In comparison, storing an object type in the Hash type Compared with storing in String type class, it takes up less memory space and facilitates access to the entire object.

In Redis, the hash type refers to the key value itself which is a key-value pair structure, in the form: value={{field1,value1},{field2,value2},{fieldN,valueN}} ,

Commonly used commands:

hget,hset,hgetall, etc.

Application scenario:

Let’s give a simple example to describe the application scenario of Hash. For example, we want to store a user information object data, including the following information:

User ID , for the search key, the value user object stored in

contains name, age, birthday and other information.

If you use an ordinary key/value structure to store, there are two main storage methods:

The first method uses the user ID as the search key and encapsulates other information into an object. Stored in a serialized manner,

such as: set u001 "李三,18,20010101"

The disadvantage of this method is that it increases the overhead of serialization/deserialization, and in When one of the pieces of information needs to be modified, the entire object needs to be retrieved, and the modification operation needs to protect concurrency, introducing complex issues such as CAS.

The second method is to store as many key-value pairs as there are members in the user information object, and use the name of the attribute corresponding to the user ID as a unique identifier to obtain the value of the corresponding attribute, such as: mset user: 001:name "李三 "user:001:age18 user:001:birthday "20010101"

Although serialization overhead and concurrency issues are eliminated, the user ID is stored repeatedly. If there is a large amount of such data , the memory waste is still very considerable.

Then the Hash provided by Redis solves this problem very well. The Hash of Redis actually stores the value internally as a HashMap, and provides an interface for direct access to the members of this Map.

For example: hmset user:001 name "李三" age 18 birthday "20010101"

That is to say, the key is still the user ID, the value is a Map, and the key of this Map is a member The attribute name, value is the attribute value, so that the modification and access to the data can be directly through the Key of its internal Map (the key of the internal Map is called field in Redis), that is, through the key (user ID) field (attribute label) ) operates the corresponding attribute data. There is no need to store data repeatedly, and it does not cause problems with serialization and concurrent modification control. Solved the problem very well. It should also be noted here that Redis provides an interface (hgetall) to directly obtain all attribute data. However, if there are many members of the internal Map, it involves the operation of traversing the entire internal Map. Due to the single-threaded model of Redis, this traversal operation It may be more time-consuming, and other client requests will not respond at all, which requires special attention.

Implementation method: As mentioned above, the Redis Hash corresponding to the Value is actually a HashMap. In fact, there are two different implementations. When the Hash has fewer members, Redis will use a one-dimensional array in order to save memory. To compactly store, instead of using the real HashMap structure, the encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will automatically be converted into a real HashMap, and the encoding is ht.

For more Redis related knowledge, please visit the Redis usage tutorial column!

The above is the detailed content of When does redis use hash type?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is the API of redisNext article:What is the API of redis