redis—Hash hash

Table of contents

Preface

1. Common commands

1.1 Command summary

1.2 Internal encoding

2.Usage scenarios


Preface

Almost all mainstream programming languages ​​provide hash types, which may be called hashes, dictionaries, associative arrays, and maps. In Redis, the hash type refers to the value itself which is a key-value pair structure, in the form of key= "key", value={ { field1 , value1 }, ... {fieldN, valueN }}, Redis key-value pair The relationship between it and the hash type can be represented by Figure 2-15. Figure 2-15 Comparison of string and hash types

The mapping relationship in the hash type is usually called field-value, which is used to distinguish the overall key-value pair of Redis. Note that the value here refers to the value corresponding to the field, not the value corresponding to the key (key). Please Pay attention to the role of value in different contexts.

1. Common commands

HSET
sets the value of the field specified in the hash.
grammar:

HSET key field value [field value ...] 

Valid version of the command: after 2.0.0
Time complexity: inserting a group of fields is 0(1), inserting N groups of fields is O(N)
Return value: the number of added fields.
Example:

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HGET myhash field1
"Hello"

HGET
gets the value of the specified field in the hash.
grammar:

HGET key field 

Valid version of the command: after 2.0.0
Time complexity: O(1)
Return value: the value corresponding to the field or nil.
Example:

redis> HSET myhash field1 "foo"
(integer) 1
redis> HGET myhash field1
"foo"
redis> HGET myhash field2
(nil)

HEXISTS
determines whether the specified field exists in the hash.
grammar:

HEXISTS key field 

Valid version of the command: after 2.0.0
Time complexity: O(1)
Return value: 1 means it exists, 0 means it does not exist.
Example:

redis> HSET myhash field1 "foo"
(integer) 1
redis> HEXISTS myhash field1
(integer) 1
redis> HEXISTS myhash field2
(integer) 0

HDEL
deletes the fields specified in the hash.
grammar:

HDEL key field [field ...]

Valid version of the command: after 2.0.0
Time complexity: deleting one element is 0(1). deleting N elements is O(N).
Return value: the number of fields deleted by this operation.
Example:

redis> HSET myhash field1 "foo"
(integer) 1
redis> HDEL myhash field1
(integer) 1
redis> HDEL myhash field2
(integer) 0

HKEYS
gets all fields in the hash.
grammar:

HKEYS key 

Valid version of the command: after 2.0.0
Time complexity: O(N), N is the number of fields.
Return value: field list.
Example:

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HKEYS myhash
1) "field1"
2) "field2"

HVALS 
gets all the values ​​in the hash.
grammar:

HVALS key 

Valid version of the command: after 2.0.0
Time complexity: O(N), N is the number of fields.
Return value: all values.
Example:

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HVALS myhash
1) "Hello"
2) "World"

HGETALL
gets all fields and corresponding values ​​in the hash.
grammar:

HGETALL key 

Valid version of the command: after 2.0.0
Time complexity: O(N), N is the number of fields.
Return value: fields and corresponding values.
Example:

redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HGETALL myhash
1) "field1"
2) "Hello"
3) "field2"
4) "World"

HMGET
obtains the values ​​of multiple fields in the hash at one time.
grammar:

HMGET key field [field ...] 

Valid version of the command: after 2.0.0
Time complexity: querying only one element is 0(1), querying multiple elements is O(N), N is the number of query elements.
Return value: the value corresponding to the field or nil. 
Example :

redis> HSET myhash field1 "Hello" 
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HMGET myhash field1 field2 nofield
1) "Hello"
2) "World"
3) (nil)

When using HGETALL, if the number of hash elements is relatively large, Redis may be blocked. If developers only need to obtain some fields, they can use HMGET. If they must obtain all fields, they can try to use the HSCAN command. This command uses a progressive traversal hash type. HSCAN will be introduced in subsequent chapters.

HLEN
gets the number of all fields in the hash.
grammar:

HLEN key 
redis> HSET myhash field1 "Hello"
(integer) 1
redis> HSET myhash field2 "World"
(integer) 1
redis> HLEN myhash
(integer) 2

HSETNX
sets the field and value in the hash if the field does not exist.
grammar:

HSETNX key field value 

Valid version of the command: after 2.0.0
Time complexity: 0(1)
Return value: 1 means the setting is successful, 0 means failure.
Example:

redis> HSETNX myhash field "Hello"
(integer) 1
redis> HSETNX myhash field "World"
(integer) 0
redis> HGET myhash field
"Hello"

HINCRBY
adds the specified value to the value corresponding to the field in the hash.
grammar:

HINCRBY key field increment 

Valid version of the command: after 2.0.0
Time complexity: 0(1)
Return value: The value after the field changes.
Example:

redis> HSET myhash field 5
(integer) 1
redis> HINCRBY myhash field 1
(integer) 6
redis> HINCRBY myhash field -1
(integer) 5
redis> HINCRBY myhash field -10
(integer) -5

HINCRBYFLOAT
The floating point version of HINCRBY.
grammar:

HINCRBYFLOAT key field increment 

Valid version of the command: after 2.6.0
Time complexity: 0(1)
Return value: The value after the field changes.
Example:

redis> HSET mykey field 10.50
(integer) 1
redis> HINCRBYFLOAT mykey field 0.1
"10.6"
redis> HINCRBYFLOAT mykey field -5
"5.6"
redis> HSET mykey field 5.0e3
(integer) 0
redis> HINCRBYFLOAT mykey field 2.0e2
"5200"

1.1 Command summary

Table 2-4 shows the effects and time complexity of hash type commands. Developers can refer to this table to
choose the appropriate command based on their own business needs and data size.
Table 2-4 Summary of hash type commands

1.2 Internal encoding

There are two internal encodings of hash:
ziplist (compressed list): when the number of hash type elements is less than hash-max-ziplist-entries configuration (default 512),
and all values ​​are less than hash-max-ziplist-value configuration (default 64 bytes), Redis will use ziplist as
the internal implementation of hashing. ziplist uses a more compact structure to achieve continuous storage of multiple elements, so it
is better than hashtable in saving memory.
●hashtable (hash table): When the hash type cannot meet the conditions of ziplist, Redis will use hashtable as the
internal implementation of hash, because the reading and writing efficiency of ziplist will decrease at this time, and the reading and writing time complexity of hashtable is 0(1).
The following example demonstrates the internal encoding of the hash type, and how the response changes.
1) When the number of fields is relatively small and there is no large value, the internal encoding is ziplist:

127.0.0.1:6379> hmset hashkey f1 v1 f2 v2
OK
127.0.0.1:6379> object encoding hashkey
"ziplist"

2) When a value is larger than 64 bytes, the internal encoding will be converted to hashtable:

127.0.0.1:6379> hset hashkey f3 "one string is bigger than 64 bytes ... 省略 ..." 1
OK
127.0.0.1:6379> object encoding hashkey
"hashtable"

3) When the number of fields exceeds 512, the internal encoding will also be converted to hashtable: 

127.0.0.1:6379> hmset hashkey f1 v1 h2 v2 f3 v3 ... 省略 ... f513 v513
OK
127.0.0.1:6379> object encoding hashkey
"hashtable"

2.Usage scenarios

Figure 2-16 shows two pieces of user information recorded in a relational data table. The user's attributes are represented as columns of the table, and each piece of user information is represented as a row. If the mapping relationship represents these two user information, it is as shown in Figure 2-17.
Figure 2-16 Relational data table saves user information

Figure 2-17 Mapping relationship represents user information

Compared to caching user information using JSON-formatted strings, the hash type becomes more intuitive and more flexible in update operations
. Each user's ID can be defined as a key suffix, and multiple field-value pairs correspond to each attribute of the user, similar to the following pseudo code
:

●The hash type is sparse, while the relational database is completely structured. For example, each key of the hash type can have different fields, and
once a new column is added to the relational database, all rows must set values ​​for it. Even if it is null, as shown in Figure 2-18.
●Relational databases can perform complex relational queries, but it is basically impossible for Redis to simulate complex relational queries, such as joint table queries and aggregation queries, and the maintenance cost is high.
Figure 2-18 Sparseness of relational database

Comparison of caching methods
So far, we have been able to use three methods to cache user information. The implementation methods and advantages and disadvantages of the three solutions are
analyzed below.
1. Native string type - use string type, one key for each attribute.

set user:1:name James
set user:1:age 23
set user:1:city Beijing

Advantages: Simple to implement and very flexible for individual attribute changes.
Disadvantages: It occupies too many keys and takes up a large amount of memory. At the same time, user information is scattered in Redis and lacks cohesion, so this
solution is basically not practical.
2. Serialize string type, such as JSON format

set user:1 经过序列化后的⽤⼾对象字符串 

Advantages: It is more suitable for information that is always operated as a whole, and programming is simple. At the same time, if the serialization scheme is properly chosen, memory usage is very efficient.
Disadvantages: Serialization and deserialization itself require a certain amount of overhead, and it is very inflexible if individual attributes are always manipulated.

3.Hash type:

hmset user:1 name James age 23 city Beijing 

Advantages: Simple, intuitive and flexible. Especially for local changes or acquisition operations of information.
Disadvantages: It is necessary to control the conversion of hash between ziplist and hashtable internal encodings, which may cause a large consumption of memory.

Guess you like

Origin blog.csdn.net/qq_65307907/article/details/134574372