Redis教程(七)_Redis 哈希(Hash)命令

Redis 哈希(Hash)

1、HDEL key field1 [field2]

删除一个或多个哈希表字段

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"
5) "description"
6) "nosql"
127.0.0.1:6379> HDEL runhashkey description
(integer) 1
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"

2、HEXISTS key field

查看哈希表 key 中,指定的字段是否存在。

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HEXISTS runhashkey name
(integer) 1
127.0.0.1:6379> HEXISTS runhashkey name1
(integer) 0

3、HGET key field

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HGET runhashkey value
"100"

4、HGETALL key

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"
5) "description"
6) "nosql"

5、HINCRBY key field increment

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HINCRBY runhashkey value 1
(integer) 101

6、HINCRBYFLOAT key field increment

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HINCRBYFLOAT runhashkey value 10.1
"110.1"

7、HKEYS key

获取所有哈希表中的字段

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HKEYS runhashkey
1) "name"
2) "value"
3) "description"

8、HLEN key

获取哈希表中字段的数量

127.0.0.1:6379> HKEYS runhashkey
1) "name"
2) "value"
3) "description"
127.0.0.1:6379> HLEN runhashkey
(integer) 3

9、HMGET key field1 [field2]

获取所有给定字段的值

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HMGET runhashkey name
1) "redis"

10、HMSET key field1 value1 [field2 value2 ]

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"
5) "description"
6) "nosql"

11、HSET key field value

将哈希表 key 中的字段 field 的值设为 value 。

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"
5) "description"
6) "nosql"

12、HSETNX key field value

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

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HSETNX runhashkey value 10
(integer) 0
127.0.0.1:6379> HSETNX runhashkey value1 10
(integer) 1
127.0.0.1:6379> HGETALL runhashkey
1) "name"
2) "redis"
3) "value"
4) "100"
5) "description"
6) "nosql"
7) "value1"
8) "10"

13、HVALS key

获取哈希表中所有值。

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HVALS runhashkey
1) "redis"
2) "100"
3) "nosql"

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

迭代哈希表中的键值对。

127.0.0.1:6379> HMSET runhashkey name redis value 100 description nosql
OK
127.0.0.1:6379> HSCAN runhashkey 0
1) "0"
2) 1) "name"
   2) "redis"
   3) "value"
   4) "100"
   5) "description"
   6) "nosql"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值