First, overview:
We can consider the hashes type in Redis as a map container with string key and string value. So this type is ideal for storing information about a value object. such as username, password and age. If the hash contains few fields, the data of that type will take up only a small amount of disk space. Each hash can store 4,294,967,295 key-value pairs.
Ii. List of related commands:
Command prototypes |
Complexity of Time |
Command description |
return value |
hset key field value |
O (1) |
Sets the field/value pair for the specified key, and if the key does not exist, the command creates a new key with a field/value pair in the argument, and overwrites its original value with the new value if the field in the argument already exists in the key. |
1 means that the new field is set to the new value, and 0 indicates that the field already exists, overwriting the original value with the new value. |
hget key field |
O (1) |
Returns the associated value of the specified field in the specified key. |
Returns the value associated with the field in the parameter, and returns nil if the key or field in the argument does not exist. |
hexistskey field |
O (1) |
Determines whether the specified field exists in the specified key. |
1 indicates existence, and 0 indicates that the field or key in the parameter does not exist. |
Hlen Key |
O (1) |
Gets the number of field that the key contains. |
Returns the field number that the key contains and returns 0 if the key does not exist. |
hdel key field [field ...] |
O (N) |
N in time complexity represents the number of fields to be deleted in the parameter. Deletes multiple fields specified in the parameter from hashes value of the specified key, if the nonexistent field is ignored. If key does not exist, it is treated as an empty hashes and returns 0. |
The number of field that was actually deleted. |
hsetnxkey field value |
O (1) |
The command does nothing if the key or field in the argument does not exist, Field/value is set for the specified key. |
1 means that the new field is set to a new value, and 0 indicates that the key or field already exists, and that the command does nothing. |
hincrbykey field increment |
O (1) |
Increases the value of the value that is associated with the specified field in the specified key. If the key or field does not exist, the command creates a new key or field, initializes its associated value to 0, and then specifies the number increment operation. The number supported by this command is a 64-bit signed integer, that is, increment can be negative. |
Returns the value after the operation. |
HgetallKey |
O (N) |
N In time complexity indicates the number of field the key contains. Gets all the field/value that the key contains. The return format is a field, a value, and so on. |
List of Field/value. |
HkeysKey |
O (N) |
N In time complexity indicates the number of field the key contains. Returns all fields names for the specified key. |
The list of field. |
hvalsKey |
O (N) |
N In time complexity indicates the number of field the key contains. Returns all values for the specified key. |
The list of value. |
hmgetkey field [field ...] |
O (N) |
N In time complexity indicates the number of field requests. Gets the set of values associated with the specified fields in the parameter. If the requested field does not exist, its value returns to nil. If the key does not exist, the command treats it as an empty hash and therefore returns a set of nil. |
Returns a set of values associated with the request fields, whose return order is equivalent to the order of fields requests. |
hmset key field value [field value ...] |
O (N) |
N in the time complexity represents the number of field that is set. Set the Field/value pairs in the parameters sequentially. If one of the field already exists, overwrite the original value with the new value. If the key does not exist, create a new key and set the Field/value in the parameter. |
|
Third, the command example:
1. Hset/hget/hdel/hexists/hlen/hsetnx:
Copy Code code as follows:
#在Shell命令行启动Redis客户端程序
/> REDIS-CLI
#给键值为myhash的键设置字段为field1, the value is Stephen.
Redis 127.0.0.1:6379> hset myhash field1 "Stephen"
(integer) 1
#获取键值为myhash, the value of the field is field1.
Redis 127.0.0.1:6379> hget Myhash field1
"Stephen"
#myhash键中不存在field2字段, so return nil.
Redis 127.0.0.1:6379> hget Myhash field2
(nil)
#给myhash关联的Hashes值添加一个新的字段field2, whose value is Liu.
Redis 127.0.0.1:6379> hset myhash field2 "Liu"
(integer) 1
#获取myhash键的字段数量.
Redis 127.0.0.1:6379> Hlen Myhash
(integer) 2
#判断myhash键中是否存在字段名为field1的字段, the return value is 1 because it exists.
Redis 127.0.0.1:6379> hexists Myhash field1
(integer) 1
#删除myhash键中字段名为field1的字段, the deletion succeeded in returning 1.
Redis 127.0.0.1:6379> Hdel Myhash field1
(integer) 1
#再次删除myhash键中字段名为field1的字段, because the previous command has deleted it, it returns 0 because it has not been deleted.
Redis 127.0.0.1:6379> Hdel Myhash field1
(integer) 0
#判断myhash键中是否存在field1字段, because the last command has deleted it because it returns 0.
Redis 127.0.0.1:6379> hexists Myhash field1
(integer) 0
#通过hsetnx命令给myhash添加新字段field1, whose value is Stephen because the field has been deleted, so the command adds success and returns 1.
Redis 127.0.0.1:6379> hsetnx Myhash field1 Stephen
(integer) 1
#由于myhash的field1字段已经通过上一条命令添加成功, because this command returns 0 after no action is made.
Redis 127.0.0.1:6379> hsetnx Myhash field1 Stephen
(integer) 0
2. Hincrby:
Copy Code code as follows:
#删除该键 to facilitate the testing of the following example.
Redis 127.0.0.1:6379> del Myhash
(integer) 1
#准备测试数据, the Field field of the Myhash is set to a value of 1.
Redis 127.0.0.1:6379> hset myhash Field 5
(integer) 1
#给myhash的field字段的值加1, returns the result of the addition.
Redis 127.0.0.1:6379> hincrby myhash Field 1
(integer) 6
#给myhash的field字段的值加-1, returns the result of the addition.
Redis 127.0.0.1:6379> Hincrby Myhash field-1
(integer) 5
#给myhash的field字段的值加-10, returns the result of the addition.
Redis 127.0.0.1:6379> Hincrby Myhash field-10
(integer)-5
3. Hgetall/hkeys/hvals/hmget/hmset:
Copy Code code as follows:
#删除该键 for later sample testing.
Redis 127.0.0.1:6379> del Myhash
(integer) 1
#为该键myhash, set multiple fields at once, respectively, field1 = "Hello", field2 = "World".
Redis 127.0.0.1:6379> hmset myhash field1 "Hello" field2 "World"
Ok
#获取myhash键的多个字段, where field3 does not exist because the value corresponding to the field in the returned result is nil.
Redis 127.0.0.1:6379> hmget myhash field1 field2
1) "Hello"
2) "World"
3) (nil)
#返回myhash键的所有字段及其值, as can be seen from the results, they are listed on a per-pair basis.
Redis 127.0.0.1:6379> Hgetall Myhash
1) "Field1"
2) "Hello"
3) "Field2"
4) "World"
#仅获取myhash键中所有字段的名字.
Redis 127.0.0.1:6379> Hkeys Myhash
1) "Field1"
2) "Field2"
#仅获取myhash键中所有字段的值.
Redis 127.0.0.1:6379> hvals Myhash
1) "Hello"
2) "World"