Hash Common Command Review
Command |
Function |
Hset key field value |
Set hash value |
Hsetnx |
Set hash value, field or key must not exist |
Hget |
Gets the value of a file |
Hdel |
Delete one or more field-value |
Hlen |
Calculate the number of field |
Hmset |
Batch setup Field-value |
Hmget |
Get Field-value in bulk |
Hexists |
Determine if a field exists |
Hkeys |
Get all the field |
Hvals |
Get all the value |
Hgetall |
Get all the Field-value |
Hincrby |
Self-increment integer |
Hincrbyfloat |
Self-increment floating point number |
Hstrlen |
Calculates the length of the value string |
Almost all programming languages provide a hash (hash) type, which may be called a hash, a dictionary, and an associative array.
In Redis, the hash type refers to the key value itself as a key-value pair structure , such as value={{field1,value1}, ... {Fieldn,valuen}},
The relationship between the Redis key-value pair and the hash type can be used to represent:
Note: The mapping relationship in the hash type is called field-value, note that value here refers to the field corresponding to the value, not the key corresponding to the value , be sure to pay attention to the value of the role in different contexts.
Some of the common commands for hashing are described below.
1) Set the value
Command:hset key field value
127.0. 0.1:6379> Hset User:11127.0. 0.1:6379> Hset User:11
If the setting succeeds returns 1, the reverse returns 0.
In addition, Redis provides the HSETNX command, which has the same relationship as the set and SETNX commands, except that the scope is changed from the key to field.
127.0.0.1:6379> hexists User:1name (integer)1#user:1Name exists127.0.0.1:6379> HSETNX User:1name Xiaoniao (integer)0#没有设置成功127.0.0.1:6379> Hget User:1name"Kebi"127.0.0.1:6379> HSETNX User:4name Xiaoniao (integer)1127.0.0.1:6379> Hget User:4name"Xiaoniao"#field不存在才行
2) Get the value
Command:hget key Field
127.0. 0.1:6379> Hget User:1 name"kebi" 127.0. 0.1:6379> Hget User:1 Dorm (nil)
If the key field does not exist or the key itself does not exist, nil is returned.
3) Delete Field
Command:hdel key field [field ...]
Hdel deletes one or more field fields, returning the number of field results that were successfully deleted:
127.0.0.1:6379> Hkeys User:11)"name"2)" Age"3)"Sex"127.0.0.1:6379> Hdel User:1name (integer)1127.0.0.1:6379> Hkeys User:11)" Age"2)"Sex"
Note If you want to delete the key or use del:
127.0. 0.1:6379> del User:11127.0. 0.1:6379> exists User:10
It does not say that the hash value can be deleted individually because it is meaningless.
4) Calculate the number of field
Command:hlen key
127.0.0.1:6379> Hset User:1name Kebi (integer)1127.0.0.1:6379> Hset User:1Age -(integer)1127.0.0.1:6379> Hset User:1sex Boy (integer)1127.0.0.1:6379> Hlen User:1(integer)3
5) batch set up or get Field-value
Command:
Hmget key field [field ...] Hmset key field value [field value ...]
Hmset and Hmget are batch setup and get Field-value,hmset required parameters are key and many pairs of field-value,hmget required parameters are key and multiple field:
127.0.0.1:6379> Hmget User:1name Age Sex1)"Kebi"2)" -"3)" Boy"127.0.0.1:6379> Hmset User:2Name Maoxian Age -Sex Boyok127.0.0.1:6379> Hmget User:2name Age Sex1)"Maoxian"2)" -"3)" Boy"
6) Determine if field exists
Command:hexists key Field
127.0. 0.1:6379> Hexists User:11127.0. 0.1:6379> Hexists User:10
If field exists, returns a result of 1 if no return 0 exists.
7) Get all the field
Command:hkeys key
The Hkeys command should be called hfields more appropriately, and it returns all field of the specified hash key:
127.0.0.1:6379> Keys *#返回所有的键1)"User:2"2)"user:1"127.0.0.1:6379> Hkeys User:1#返回键user: 1 of all field1)"name"2)" Age"3)"Sex"127.0.0.1:6379> Hkeys User:21)"name"2)" Age"3)"Sex"
8) Get all value
Command:hvals key
127.0.0.1:6379> Hvals User:11)"Kebi"2)" -"3)" Boy"127.0.0.1:6379> Hvals User:21)"Maoxian"2)" -"3)" Boy"
9) Get all the Field-value
Command:hgetall key
127.0.0.1:6379> Hgetall User:11)"name"2)"Kebi"3)" Age"4)" -"5)"Sex"6)" Boy"
Note: When using Hgetall, there is a possibility of blocking Redis if the number of hash elements is more.
If you only need to get part of the field, you can use Hmget, if you must go back to all field-value, you can use the Hscan command, which will iterate through the hash type incrementally.
Ten) Hincrby Hincrbyfloat
Command:
Hincrby Key fieldhincrbyfloat key field
The Hincrby and Hincrbyfloat commands are the same, but their scope is field.
11) Calculates the character length of value (requires Redis3.2 version or more)
Command:hstrlen key Field
The following provides the time complexity of a hash type command, which can be used to select the appropriate command according to its own requirements.
the time complexity of the hash type command
Command |
Complexity of Time |
Hset key field value |
O (1) |
Hget key Field |
O (1) |
Hdel key field [field ...] |
O (k), K is field number |
Hlen Key |
O (1) |
Hgetall Key |
O (n), n is the total number of field |
Hmget field [Field ...] |
O (k), K is field number |
Hmset field value [Field value ...] |
O (k), K is field number |
Hexists key Field |
O (1) |
Hkeys Key |
O (n), n is the total number of field |
Hvals Key |
O (n), n is the total number of field |
Hsetnx key field value |
O (1) |
Hincrby key field Increment |
O (1) |
Hincrbyfloat key field Increment |
O (1) |
Hstrlen key Field |
O (1) |
Understanding and use of APIs--hash type commands