redis之Hash类型常用方法总结
格式:
存--HMGET key field [field ...]
取--HMGET key field [field ...]
M:表示能取多个值,many
H:表示hash类型
基本操作
-
hmset key field value [field value ...] 设置hash字段和值
127.0.0.1:6379> hmset hehe_key name "panlifu" age "100" OK 127.0.0.1:6379> hgetall hehe_key 1) "name" 2) "panlifu" 3) "age" 4) "100" 127.0.0.1:6379> hmget hehe_key name 1) "panlifu" 127.0.0.1:6379> hmget hehe_key age 1) "100" 127.0.0.1:6379>
-
hmget key field [field ...] 获取哈希表中某个字段的值
127.0.0.1:6379> hmset hehe_key name "panlifu" age "100" OK 127.0.0.1:6379> hmget hehe_key name 1) "panlifu" 127.0.0.1:6379> hmget hehe_key age 1) "100" 127.0.0.1:6379>
-
hgetall key 获取哈表表中所有字段的值及字段
127.0.0.1:6379> hmset hehe_key name "panlifu" age "100" OK 127.0.0.1:6379> hgetall hehe_key 1) "name" 2) "panlifu" 3) "age" 4) "100"
-
hdel key field [field ...] 删除一个或多个哈希表字段
127.0.0.1:6379> hmset hehe_key name "panlifu" age "100" OK 127.0.0.1:6379> hmget hehe_key name 1) "panlifu" 127.0.0.1:6379> hmget hehe_key age 1) "100" 127.0.0.1:6379> hdel hehe_key name (integer) 1 127.0.0.1:6379> hmget hehe_key name 1) (nil)
-
hexists key field 查看哈希表中,指定的字段是否存在,存在返回1,不存在(哈希表或指定字段不存在)返回0
127.0.0.1:6379> hmset hehe name panlifu sex boy OK 127.0.0.1:6379> hexists keke name (integer) 0 127.0.0.1:6379> hexists hehe name (integer) 1
-
hget key field 获取存储在哈希表中指定字段的值
127.0.0.1:6379> hmset hehe_key name "panlifu" age "100" OK 127.0.0.1:6379> hgetall hehe_key 1) "name" 2) "panlifu" 3) "age" 4) "100" 127.0.0.1:6379> hmget hehe_key name 1) "panlifu" 127.0.0.1:6379> hmget hehe_key age 1) "100" 127.0.0.1:6379>
-
hset key field 设置存储在哈希表中指定字段的值(只能存储一个)
127.0.0.1:6379> hset hehe age 10 (integer) 0 127.0.0.1:6379> hget hehe age "10"
-
hincrby key field increment 为哈希表 key 中的指定字段的整数值加上增量 increment
127.0.0.1:6379> hset hehe age 10 (integer) 0 127.0.0.1:6379> hget hehe age "10" 127.0.0.1:6379> hincrby hehe age 1 # hash表hehe中的age字段的值增加1 (integer) 11 127.0.0.1:6379> hget hehe age "11"
-
hincrbyfloat key field increment 为哈希表 key 中的指定字段的浮点数值加上增量 increment 。
127.0.0.1:6379> hset hehe height 1.8 (integer) 1 127.0.0.1:6379> hincrbyfloat hehe height 0.5 "2.3" 127.0.0.1:6379> hget hehe height "2.3"
-
hkeys key 获取所有哈希表中的字段
127.0.0.1:6379> hkeys hehe 1) "name" 2) "age" 3) "sex" 4) "height"
-
hlen key 获取哈希表中字段的数量
127.0.0.1:6379> hkeys hehe 1) "name" 2) "age" 3) "sex" 4) "height" 127.0.0.1:6379> hlen hehe (integer) 4
-
hsetnx key field value 只有在字段 field 不存在时,设置哈希表字段的值。
127.0.0.1:6379> hgetall hehe 1) "name" 2) "panlifu" 3) "age" 4) "11" 5) "sex" 6) "boy" 7) "height" 8) "2.3" 127.0.0.1:6379> hsetnx hehe name lt (integer) 0 127.0.0.1:6379> hgetall hehe 1) "name" 2) "panlifu" 3) "age" 4) "11" 5) "sex" 6) "boy" 7) "height" 8) "2.3"
-
hvals key 获取哈希表中所有值
127.0.0.1:6379> hgetall hehe 1) "name" 2) "panlifu" 3) "age" 4) "11" 5) "sex" 6) "boy" 7) "height" 8) "2.3" 127.0.0.1:6379> hvals hehe 1) "panlifu" 2) "11" 3) "boy" 4) "2.3"
-
HSCAN key cursor [MATCH pattern] [COUNT count] 迭代哈希表中的键值对。
# 1. 查看hash表pms:1中有多少条记录 127.0.0.1:6379[1]> hgetall pms:1 1) "stock" 2) "12" 3) "freeze" 4) "10" 5) "stock:1" 6) "11" 7) "stock:2" 8) "23" 9) "stock:freeze:1" 10) "111" 11) "stock:5" 12) "1212" # 2. 模糊查看pms:1下的键 127.0.0.1:6379[1]> hscan pms:1 0 match stock:* count 100 1) "0" 2) 1) "stock:1" 2) "11" 3) "stock:2" 4) "23" 5) "stock:freeze:1" 6) "111" 7) "stock:5" 8) "1212" # 3. 模糊查看pms:1下的键 127.0.0.1:6379[1]> hscan pms:1 0 match stock* count 100 1) "0" 2) 1) "stock" 2) "12" 3) "stock:1" 4) "11" 5) "stock:2" 6) "23" 7) "stock:freeze:1" 8) "111" 9) "stock:5" 10) "1212" # 4. 模糊查看pms:1下的键 127.0.0.1:6379[1]> hscan pms:1 0 match stock:freeze:* count 100 1) "0" 2) 1) "stock:freeze:1" 2) "111" # 5. 查看有多少个键 127.0.0.1:6379[1]> keys * 1) "pms:1" 2) "pms:freeze:21" 3) "pms:10" 4) "pms:11" 5) "pms:9" 6) "pms:4" 7) "name" 8) "pms:5" 9) "pms:8" 10) "pms:7" 11) "pms:2" 12) "go:123" 13) "pms:3" 14) "pms:6" # 6. 模糊查找pms:*的键 127.0.0.1:6379[1]> scan 0 match pms:* count 10 1) "3" 2) 1) "pms:1" 2) "pms:8" 3) "pms:freeze:21" 4) "pms:5" 5) "pms:11" 6) "pms:9" 7) "pms:4" 8) "pms:3" 9) "pms:6" # 7. 模糊查找pms:*的键,游标从3开始 127.0.0.1:6379[1]> scan 3 match pms:* count 10 1) "0" 2) 1) "pms:10" 2) "pms:7" 3) "pms:2