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"