Redis data type (hash type)

hash type and operation

① Overview

The hash type in Redis can be understood as a map container with String Key and String Value, so the hash type is suitable for storing value object information. Such as Username, Password or Age. Even if there are only a few fields in the hash, its data only takes up a small amount of disk space. Each hash can store 4294967295 key value pairs.

② List of related commands

click here click here click here click here
Command prototypeTime complexityCommand descriptionReturn value
HSET key field valueO(1)Set the Field/Value pair for the specified Key. If the Key does not exist, the command will create a new Key to the Field/Value pair in the parameter. If the Field in the parameter already exists in the Key, the original value will be overwritten with the new value.1 indicates that the new Field has been set with a new value, 0 indicates that the Field already exists, and the original value is overwritten with the new value.
HGET key fieldO(1)Returns the associated value of the specified Field in the specified Key.Returns the associated value of the Field in the parameter. If the Key or Field in the parameter does not exist, nil is returned.
HEXISTS key fieldO(1)Judge whether the specified Field in the specified Key exists.1 indicates existence, and 0 indicates that the Field or Key in the parameter does not exist.
HLEN keyO(1)Get the number of fields contained in the Key.Returns the number of fields contained in the Key. If the Key does not exist, it returns 0.
HDEL key field [field ...]O(N)N in time complexity indicates the number of fields to be deleted in the parameter. Delete multiple fields specified in the parameter from the Hashes Value of the specified Key. If the fields do not exist, they will be ignored. If the Key does not exist, it is regarded as empty Hashes and returns 0Number of fields actually deleted.
HSETNX key field valueO(1)Only when the Key or Field in the parameter does not exist, set the Field/Value pair for the specified Key, otherwise the command will not carry out any operation.1 indicates that the new Field is set with a new value, 0 indicates that the Key or Field already exists, and this command does not perform any operation.
HINCRBY key field incrementO(1)Increase the Value of the Value associated with the specified Field in the specified Key. If the Key or Field does not exist, the command will create a new Key or Field, initialize its associated Value to 0, and then specify the operation of increasing the number. The number supported by this command is 64 bit signed integer, that is, increment can be negative.Returns the calculated value.
HGETALL keyO(N)N in the time complexity indicates the number of fields contained in the Key. Gets all Field / values contained in the Key. The return format is a Field, a Value, and so on.List of field / values.
HKEYS keyO(N)N in the time complexity indicates the number of Fields contained in the Key. Returns all Fields names of the specified Key.List of fields.
HVALS keyO(N)N in the time complexity indicates the number of fields contained in the Key. Returns all Values names of the specified Key.List of values.
HMGET key field [field ...]O(N)N in the time complexity indicates the number of Fields requested. Gets a set of Values associated with the Fields specified in the parameter. If the requested Field does not exist, its value returns nil. If the Key does not exist, the command treats it as an empty Hash and returns a set of NILs.Returns a set of Values associated with the request Fields, whose return order is equal to the request order of Fields.
HMSET key field value [field value ...]O(N)N in the time complexity indicates the number of fields set. Set the Field/Value pairs given in the parameters one by one. If one of the fields already exists, the original value is overwritten with the new value. If the Key does not exist, create a new Key and set the Field/Value in the parameter.

③ Command example

1. HSET/HGET/HDEL/HEXISTS/HLEN/HSETNX:
127.0.0.1:6379> hset myhash field1 "JohnnyG"
(integer) 1
#Create the key "myhash", set the field "field1" and assign the value "JohnnyG"

127.0.0.1:6379> hget myhash field1
"JohnnyG"
#Check the "field1" field of "myhash". You can see that the "field1" field of "myhash" has been successfully created and the value is "Johnny g"

127.0.0.1:6379> hget myhash field2
(nil)
#Check the "field2" field of "myhash". Since the "field2" field is not created (no value), it returns a null value (nil)

127.0.0.1:6379> hset myhash field2 "lili"
(integer) 1
#Create the field "field2" for the key "myhash" and assign the value "lili"
127.0.0.1:6379> hget myhash field2
"lili"
#Check the "field2" field of "myhash". You can see that the "field2" field of "myhash" has been successfully created and the value is "lili"

127.0.0.1:6379> hlen myhash
(integer) 2
#Check the number of fields in the "myhash" key, and you can see that the number is 2 (field1 & field2)


127.0.0.1:6379> hexists myhash field1
(integer) 1
127.0.0.1:6379> hexists myhash field2
(integer) 1
127.0.0.1:6379> hexists myhash field3
(integer) 0
#Judge whether there are fields named "field1", "field2" and "field3" in the "myhash" key. You can see that the existing return value is 1 and the non-existent return value is 0

127.0.0.1:6379> hdel myhash field2
(integer) 1
#Delete the field named "field2" in the "myhash" key, and the deletion is successful (return 1)

127.0.0.1:6379> hdel myhash field2
(integer) 0
#Delete the field named "field2" in the "myhash" key again. Since "field2" has been deleted, the deletion fails (return 0)

127.0.0.1:6379> hexists myhash field2
(integer) 0
#Judge whether there is a field named "field2" in the myhash key. You can see that it does not exist (return 0)

127.0.0.1:6379> hlen myhash
(integer) 1
##Check the number of fields in the "myhash" key. You can see that the number is 1 (field1), and field2 has been deleted

127.0.0.1:6379> hsetnx myhash field2 "lili"
(integer) 1
#Add a new field field2 to myhash through the hsetnx command and assign the value "lili". Because the field has been deleted (does not exist), the command is executed successfully (return 1)

127.0.0.1:6379> hget myhash field2
"lili"
#Check the "field2" field of "myhash". You can see that the "field2" field of "myhash" has been successfully created and the value is "lili"

127.0.0.1:6379> hsetnx myhash field2 "joe"
(integer) 0
#Add a new field field2 to myhash through the hsetnx command and assign the value "joe". Because the field has been recreated (exists), the command fails to execute (returns 0)

127.0.0.1:6379> hget myhash field2
"lili"
#Check the "field2" field of "myhash". Because the previous command failed to execute (the "field2" field already exists), its value is still "lili" (no change)
2. HINCRBY: 
127.0.0.1:6379> del myhash
(integer) 1
#Delete "myhash" key (prepare for later test)

127.0.0.1:6379> hset myhash field 10
(integer) 1
#Create the key "myhash", set the field "field1" and assign a value of 10

127.0.0.1:6379> hget myhash field
"10"
#Check the "field1" field of "myhash". You can see that the "field1" field of "myhash" has been successfully created and the value is 10

127.0.0.1:6379> hincrby myhash field 1
(integer) 11
#Increment the value of the "field" field of "myhash" by 1 and return the result 11 after one increment

127.0.0.1:6379> hincrby myhash field -1
(integer) 10
#The value of "myhas" is decremented once to the value of "myhas" of "10"

127.0.0.1:6379> hincrby myhash field -10
(integer) 0
#Decrement the value of the "field" field of "myhash" by 10 and return the result of decrement once to 0

127.0.0.1:6379> hincrby myhash field 15
(integer) 15
#Increment the value of the "field" field of "myhash" by 15 and return the result 15 after one increment
3. HGETALL/HKEYS/HVALS/HMGET/HMSET:
127.0.0.1:6379> del myhash
(integer) 1
#Delete "myhash" key (prepare for later test)

127.0.0.1:6379> hmset myhash name "xiaoming" age 18
OK
#Create the key "myhash", set multiple fields "name" and "age" at the same time, and assign values as "name" = "xiaoming" and "age" = 18 respectively, and return OK to indicate that the creation is successful

127.0.0.1:6379> hmget myhash name age address
1) "xiaoming"
2) "18"
3) (nil)
#At the same time, check the fields "name", "age" and "address" of the "myhash" key. You can see that the values of "name" and "age" already exist (created successfully). However, since the "address" field does not exist (the value is null), a null value (nil) is returned in the return result

127.0.0.1:6379> hgetall myhash
1) "name"
2) "xiaoming"
3) "age"
4) "18"
#Check all fields and their values in the "myhash" key. From the results, you can see that the returned values are listed one by one in pairs (the values follow the corresponding fields)

127.0.0.1:6379> hkeys myhash
1) "name"
2) "age"
#View only the names of all fields in the "myhash" key

127.0.0.1:6379> hvals myhash
1) "xiaoming"
2) "18"
#View only the values of all fields in the "myhash" key

Tags: Linux Database Redis

Posted by daniel_grant on Sat, 21 May 2022 04:01:06 +0300