1. Hash
1.1 hdel
1.1.1 基本信息
HDEL key field [field ...]
summary: Delete one or more hash fields
since: 2.0.0
Removes the specified fields from the hash stored at key. 从键存储的哈希中移除指定的字段。
Specified fields that do not exist within this hash are ignored. 如果指定字段不存在,则忽略。
If key does not exist, it is treated as an empty hash and this command returns 0. 如果键不存在,则将其视为空散列,该命令返回0。
1.1.2 返回
- 从哈希中移除的字段数,不包括指定但不存在的字段数
1.1.3 练习
127.0.0.1:6379[1]> hset myhash field1 "field1" field2 "field2"
(integer) 2
127.0.0.1:6379[1]> hdel myhash field1
(integer) 1
1.2 hexists
1.2.1 基本信息
HEXISTS key field
summary: Determine if a hash field exists
since: 2.0.0
Returns if field is an existing field in the hash stored at key.
返回字段是否为键存储的散列中的现有字段。
1.2.2 返回
- 如果散列包含字段,则为1
- 如果散列不包含字段或键不存在,则返回0
1.2.3 练习
127.0.0.1:6379[1]> hexists myhash field1
(integer) 0
127.0.0.1:6379[1]> hexists myhash field2
(integer) 1
1.3 hget
1.3.1 基本信息
HGET key field
summary: Get the value of a hash field
since: 2.0.0
Returns the value associated with field in the hash stored at key.
返回存储在键上的哈希中与字段关联的值。
1.3.2 返回
- 与字段关联的值,如果字段不存在于散列或键中,则为 nil
1.3.3 练习
127.0.0.1:6379[1]> hget myhash field2
"field2"
1.4 hgetall
1.4.1 基本信息
HGETALL key
summary: Get all the fields and values in a hash
since: 2.0.0
Returns all fields and values of the hash stored at key. 返回键存储的哈希的所有字段和值。
In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.
在返回的值中,每个字段名后面都跟着它的值,因此长度是散列大小的两倍。
1.4.2 返回
- 存储在散列中的字段及其值的列表,或键不存在时的空列表
1.4.3 练习
127.0.0.1:6379[1]> hgetall myhash
1) "field2"
2) "field2"
1.5 hincrby
1.5.1 基本信息
HINCRBY key field increment
summary: Increment the integer value of a hash field by the given number
since: 2.0.0
Increments the number stored at field in the hash stored at key by increment. 按增量递增存储在键上的散列中字段中的数字。
If key does not exist, a new key holding a hash is created. If field does not exist the value is set to 0 before the operation is performed.
如果键不存在,则创建一个包含散列的新键。如果字段不存在,则在执行操作之前将该值设置为0。
The range of values supported by HINCRBY is limited to 64 bit signed integers.
HINCRBY 支持的值范围限制为64位有符号整数。
1.5.2 返回
- 增量操作后字段的值
1.5.3 练习
127.0.0.1:6379[1]> hset myhash field3 3
(integer) 1
127.0.0.1:6379[1]> hincrby myhash field3 5
(integer) 8
127.0.0.1:6379[1]> hget myhash field3
"8"
127.0.0.1:6379[1]> hincrby myhash field3 -9
(integer) -1
127.0.0.1:6379[1]> hget myhash field3
"-1"
1.6 hincrbyfloat
1.6.1 基本信息
HINCRBYFLOAT key field increment
summary: Increment the float value of a hash field by the given amount
since: 2.6.0
Increment the specified field of a hash stored at key, and representing a floating point number, by the specified increment. If the increment value is negative, the result is to have the hash field value decremented instead of incremented. If the field does not exist, it is set to 0 before performing the operation. An error is returned if one of the following conditions occur:
将存储在键上并表示浮点数的散列的指定字段增加指定的增量。如果增量值为负,则结果是散列字段值递减而不是递增。如果该字段不存在,则在执行操作之前将其设置为0。如果出现下列情况之一,则返回错误:
-
The field contains a value of the wrong type (not a string).
-
- 该字段包含错误类型的值(不是字符串)。
-
The current field content or the specified increment are not parsable as a double precision floating point number.
-
- 当前字段内容或指定的增量不能解析为双精度浮点数。
1.6.2 返回
- 字段增量后的值
1.6.3 练习
127.0.0.1:6379[1]> hincrbyfloat myhash field3 7.53
"6.53000000000000025"
127.0.0.1:6379[1]> hincrbyfloat myhash field3 -2
"4.53000000000000025"
127.0.0.1:6379[1]> hget myhash field3
"4.53000000000000025"
1.7 hkeys
1.7.1 基本信息
HKEYS key
summary: Get all the fields in a hash
since: 2.0.0
Returns all field names in the hash stored at key.
返回键存储的散列中的所有字段名。
1.7.2 返回
- 散列中的字段列表,或键不存在时的空列表
1.7.3 练习
127.0.0.1:6379[1]> hkeys myhash
1) "field2"
2) "field3"
1.8 hlen
1.8.1 基本信息
HLEN key
summary: Get the number of fields in a hash
since: 2.0.0
Returns the number of fields contained in the hash stored at key.
返回键存储的哈希中包含的字段数。
1.8.2 返回
- 散列中的字段数,或键不存在时为0
1.8.3 练习
127.0.0.1:6379[1]> hlen myhash
(integer) 2
1.9 hmget
1.9.1 基本信息
HMGET key field [field ...]
summary: Get the values of all the given hash fields
since: 2.0.0
Returns the values associated with the specified fields in the hash stored at key.
返回键存储的哈希中与指定字段关联的值。
For every field that does not exist in the hash, a nil value is returned. Because non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.
对于散列中不存在的每个字段,将返回一个 nil 值。因为不存在的键被视为空散列,对不存在的键运行 HMGET 将返回一个 nil 值列表。
1.9.2 返回
- 与给定字段关联的值列表,顺序与请求的顺序相同
1.9.3 练习
127.0.0.1:6379[1]> hmget myhash field1 field2 field3
1) (nil)
2) "field2"
3) "4.53000000000000025"
1.10 hrandfield
1.10.1 基本信息
HRANDFIELD key [count [WITHVALUES]]
summary: Get one or multiple random fields from a hash
since: 6.2.0
When called with just the key argument, return a random field from the hash value stored at key.
当仅使用 key 参数调用时,从存储在 key 上的散列值返回一个随机字段。
If the provided count argument is positive, return an array of distinct fields. The array's length is either count or the hash's number of fields (HLEN), whichever is lower.
如果提供的 count 参数为正,则返回一个包含不同字段的数组。数组的长度是 count 或 hash 的字段数(HLEN) ,以较低者为准。
If called with a negative count, the behavior changes and the command is allowed to return the same field multiple times. In this case, the number of returned fields is the absolute value of the specified count.
如果调用时计数为负,则行为将发生更改,并允许命令多次返回相同的字段。在这种情况下,返回字段的数量是指定计数的绝对值。
The optional WITHVALUES modifier changes the reply so it includes the respective values of the randomly selected hash fields.
可选的 WITHVALUES 修饰符更改应答,使其包含随机选择的散列字段的各自值。
1.10.2 返回
- 如果没有附加 count 参数,该命令将返回一个随机选择的字段
- 如果键不存在,则返回 nil
- 当传递额外的 count 参数时,命令返回一个字段数组
- 当键不存在时返回一个空数组
- 如果使用 WITHVALUES 修饰符,则是一个列表字段及其来自哈希的值
1.10.3 练习
127.0.0.1:6379[1]> hset myhash field4 "field4" field5 "field5" field6 "4444"
(integer) 3
127.0.0.1:6379[1]> hrandfield myhash 3
1) "field4"
2) "field2"
3) "field3"
1.11 hscan
1.11.1 基本信息
HSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate hash fields and associated values
since: 2.8.0
1.11.2 返回
- 散列中字段及其关联的值
1.11.3 练习
127.0.0.1:6379[1]> hscan myhash 2
1) "0"
2) 1) "field2"
2) "field2"
3) "field3"
4) "4.53000000000000025"
5) "field4"
6) "field4"
7) "field5"
8) "field5"
9) "field6"
10) "4444"
1.12 hset
1.12.1 基本信息
HSET key field value [field value ...]
summary: Set the string value of a hash field
since: 2.0.0
Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.
将存储在键的哈希中的字段设置为 value。如果键不存在,则创建一个包含散列的新键。如果字段已经存在于散列中,则将覆盖该字段。
1.12.2 返回
- 添加的字段数
1.12.3 练习
127.0.0.1:6379[1]> hset newkey f1 "111" f2 222 f3 333
(integer) 3
1.13 hsetnx
1.13.1 基本信息
HSETNX key field value
summary: Set the value of a hash field, only if the field does not exist
since: 2.0.0
Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.
仅当字段尚不存在时,才将存储在键上的散列中的字段设置为 value。如果键不存在,则创建一个包含散列的新键。如果字段已经存在,则此操作无效。
1.13.2 返回
- 如果字段是散列中的新字段且值已设置,则为1
- 如果散列中已存在字段且未执行任何操作,则为0
1.13.3 练习
127.0.0.1:6379[1]> hsetnx newkey f1 666
(integer) 0
1.14 hstrlen
1.14.1 基本信息
HSTRLEN key field
summary: Get the length of the value of a hash field
since: 3.2.0
Returns the string length of the value associated with field in the hash stored at key. If the key or the field do not exist, 0 is returned.
返回键存储的哈希中与字段关联的值的字符串长度。如果键或字段不存在,则返回0。
1.14.2 返回
- 与字段关联的值的字符串长度,如果字段不存在于哈希或键中,则字符串长度为零。
1.14.3 练习
127.0.0.1:6379[1]> hstrlen newkey f3
(integer) 3
1.15 hvals
1.15.1 基本信息
HVALS key
summary: Get all the values in a hash
since: 2.0.0
Returns all values in the hash stored at key.
返回键存储的哈希中的所有值。
1.15.2 返回
- 散列中的值列表,或键不存在时的空列表
1.15.3 练习
127.0.0.1:6379[1]> hvals newkey
1) "111"
2) "222"
3) "333"
标签:Hash,005,6379,0.1,Redis,value,field,key,hash
From: https://www.cnblogs.com/autumncat/p/16757606.html