5.Redis常用命令:Hash

时间:2023-03-09 17:53:13
5.Redis常用命令:Hash

  我们可以将Redis中的Hashes类型看成具有String Key和String Value的map容器。所以该类型非常适合于存储值对象的信息。如Username、Password和Age等。如果Hash中包含很少的字段,那么该类型的数据也将仅占用很少的磁盘空间。每一个Hash可以存储4294967295个键值对。

1、HSET key field value

将哈希表 key 中的字段 field 的值设为 value。如果字段是哈希表中的一个新建字段,并且值设置成功,返回 1 。 如果哈希表中域字段已经存在且旧值已被新值覆盖,返回 0 。

127.0.0.1:> hset website google "www.google.com"
(integer)
127.0.0.1:> hget website google
"www.google.com"
127.0.0.1:> hset website google www.google.com
(integer)
127.0.0.1:> hget website google
"www.google.com"

2、HGET key field 

获取存储在哈希表中指定字段的值

127.0.0.1:> hset website google "www.google.com"
(integer)
127.0.0.1:> hget website google
"www.google.com"
127.0.0.1:> hset website google www.google.com
(integer)
127.0.0.1:> hget website google
"www.google.com"

3、HEXISTS key field 

查看哈希表 key 中,指定的字段是否存在。如果哈希表含有给定字段,返回 1 。 如果哈希表不含有给定字段,或 key 不存在,返回 0 。

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.com"
127.0.0.1:> hexists website baidu
(integer)

4、HDEL key field1 [field2] 

删除一个或多个哈希表字段,不存在的字段将被忽略。返回值显示被成功删除字段的数量,不包括被忽略的字段。

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.com"
127.0.0.1:> hdel website baidu yahoo
(integer)
127.0.0.1:> hgetall website
) "google"
) "www.google.com"

5、HMGET key field1 [field2]

获取所有给定字段的值

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
127.0.0.1:> hmget website baidu yahoo
) "www.baidu.com"
) "www.yahoo.cn"

6、HMSET key field1 value1 [field2 value2 ]

同时将多个 field-value (域-值)对设置到哈希表 key 中。

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
127.0.0.1:> hmset website sina www.sina.com netease .com
OK
127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
) "sina"
) "www.sina.com"
) "netease"
) "163.com"

7、HKEYS key

获取所有哈希表中的字段

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
) "sina"
) "www.sina.com"
) "netease"
) "163.com"
127.0.0.1:> hkeys website
) "google"
) "baidu"
) "yahoo"
) "sina"
) "netease"

8、HVALS key

获取哈希表中所有值

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
) "sina"
) "www.sina.com"
) "netease"
) "163.com"
127.0.0.1:> hvals website
) "www.google.com"
) "www.baidu.com"
) "www.yahoo.cn"
) "www.sina.com"
) "163.com"

9、HGETALL key

获取在哈希表中指定 key 的所有字段和值

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "baidu"
) "www.baidu.com"
) "yahoo"
) "www.yahoo.cn"
) "sina"
) "www.sina.com"
) "netease"
) "163.com"

10、HINCRBY key field increment

为哈希表 key 中的指定字段的整数值加上增量 increment 。返回增加之后的结果。

127.0.0.1:> hset configuration connections
(integer)
127.0.0.1:> hincrby configuration connections
(integer)
127.0.0.1:> hget configuration connections
""

11、HINCRBYFLOAT key field increment

为哈希表 key 中的指定字段的浮点数值加上增量 increment 。

127.0.0.1:> hset configuration amount 10.33
(integer)
127.0.0.1:> hincrbyfloat configuration amount 4.67
""
127.0.0.1:> hget configuration amount
""

12、HSETNX key field value

只有在字段 field 不存在时,设置哈希表字段的值。

127.0.0.1:> hgetall website
) "google"
) "www.google.com"
127.0.0.1:> hsetnx website google hellogoogle
(integer)
127.0.0.1:> hsetnx website xiaomi www.mi.com
(integer)
127.0.0.1:> hgetall website
) "google"
) "www.google.com"
) "xiaomi"
) "www.mi.com"

13、HSCAN key cursor [MATCH pattern] [COUNT count] 

迭代哈希表中的键值对。