Redis | Redis hash related commands

Posted by Kieran Huggins on Tue, 23 Nov 2021 16:50:24 +0100

        Redis supports a variety of data structures, such as string, list, set, ordered set and hash. This time I sorted out about   Hash   Related commands, that is, about   Hashes   The related commands are as follows.

        The part in the red circle in the figure above is about   Hash   Related commands. If you want to view related commands in Redis, you can use   help   Command to view. The command is as follows.  

127.0.0.1:6379> help @hash

         After pressing enter, you can see the description of Hashes related commands, as shown in the figure below.

        The figure shows some commands related to Hashes.

Common Hashes related commands

1,hset

        This command is used to set the hash field value. The format of this command is as follows:  

hset key field value

        Examples are as follows:

127.0.0.1:6379> hset user001 name zhangsan
(integer) 1
127.0.0.1:6379> hset user001 gender male
(integer) 1
127.0.0.1:6379> hset user001 age 18
(integer) 1

        In the above command, user001 is the key, name, gender and age are the field, and zhangsan, male and 18 are the value.

        There can be multiple different fields under the same key, and each field has its own corresponding value.

2,hsetnx

         The function of this command is to set a field of hash. It will be set only when the specified field does not exist. The format of the command is as follows:

hsetnx key field value

         Examples are as follows:

127.0.0.1:6379> hsetnx user001 age 18
(integer) 0
127.0.0.1:6379> hsetnx user001 weight 70
(integer) 1

        In the above command, the age field already exists in the key user001, so the hsetnx command does not add or modify age, but weight does not exist in the key user001, so weight is added to user001   Yes.

3,hmset

        This command is used to add multiple field s and value s to the hash at one time. The format of the command is as follows:

hmset key field value [field value ...]

        Examples are as follows:

127.0.0.1:6379> hmset user002 name lisi gender femle age 20 weight 65
OK

4,hget

         The function of this command is to obtain the value of the field in the hash. The usage of this command is as follows:

127.0.0.1:6379> hget user001 name
"zhangsan"
127.0.0.1:6379> hget user001 gender
"male"

5,hgetall

         The function of this command is to obtain the values of all fields of hash. The usage of this command is as follows:

127.0.0.1:6379> hgetall user001
1) "name"
2) "zhangsan"
3) "gender"
4) "male"
5) "age"
6) "18"
7) "weight"
8) "70"
127.0.0.1:6379> hgetall user002
1) "name"
2) "lisi"
3) "gender"
4) "femle"
5) "age"
6) "20"
7) "weight"
8) "65"

6,hkeys

         The function of this command is to obtain all fields of hash without obtaining values. The usage of this command is as follows:

127.0.0.1:6379> hkeys user001
1) "name"
2) "gender"
3) "age"
4) "weight"

7,hexists

         The function of this command is to judge whether the field exists in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hexists user001 name
(integer) 1
127.0.0.1:6379> hexists user001 school
(integer) 0

8,hlen

         The function of this command is to obtain the number of fields in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hlen user001
(integer) 4
127.0.0.1:6379> hlen user002
(integer) 4

9,hmget

         The function of this command is to obtain multiple fields in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hmget user001 name age
1) "zhangsan"
2) "18"

10,hstrlen

         The function of this command is to obtain the length of the field in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hstrlen user001 name
(integer) 8
127.0.0.1:6379> hstrlen user001 age
(integer) 2

11,hvals

         The function of this command is to obtain all the values of the specified hash without obtaining the field name. The usage of this command is as follows:

127.0.0.1:6379> hvals user001
1) "zhangsan"
2) "male"
3) "18"
4) "70"

12,hincrby

         The function of this command is to add a specified integer value to the value of the specified field in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hget user001 age
"18"
127.0.0.1:6379> hincrby user001 age 2
(integer) 20

13,hincrbyfloat

         This command is used to add a specified floating point value to the value of the specified field in the specified hash. The usage of this command is as follows:

127.0.0.1:6379> hget user002 age
"20"
127.0.0.1:6379> hincrbyfloat user002 age 2.5
"22.5"
127.0.0.1:6379> hincrbyfloat user002 age 2
"24.5"

        Using hincrbyfloat, you can add a floating-point value or an integer value to a field in the hash

127.0.0.1:6379> hincrbyfloat user001 age 1
"21"
127.0.0.1:6379> hincrby user001 age 1.5
(error) ERR value is not an integer or out of range

        For hincrby, you can only increase integer values, not floating-point values

14,hdel

         The function of this command is to delete one or more fields specified in the hash. The usage of this command is as follows:

127.0.0.1:6379> hdel user001 name age
(integer) 2
127.0.0.1:6379> hgetall user001
1) "gender"
2) "male"
3) "weight"
4) "70"
127.0.0.1:6379> hdel user002 name
(integer) 1
127.0.0.1:6379> hgetall user002
1) "gender"
2) "femle"
3) "age"
4) "24.5"
5) "weight"
6) "65"

summary

        Redis's hash type provides relatively simple commands, and some of them look similar, such as hgetall and hvals. The former returns fields and values, while the latter only returns values. So why should there be commands like hvals? My own consideration is "efficiency". When we are clear about the fields stored in the hash, only returning the value without returning the field can reduce the amount of data transmitted over the network and improve the transmission efficiency. On the other hand, the format of returning fields and values is different from that of returning only values, which also saves memory space.

        The hash introduced in this article is the last of several basic data types of Redis, including string, set, ordered set and list. In the following content, I will gradually reorganize other related commands and some application scenarios of basic data structure. Moreover, I will sort out some Redis source codes I have seen and share them. I hope I can sort out what I have learned about Redis more completely to facilitate my review.

Topics: Big Data Redis Cache