Redis 哈希(Hash)
hash的格式也是键值对key:map,只不过他的值是map集合。
key:{key:vlaue}
案例
127.0.0.1:6379> HSET myhash field1 lili #set一个具体的key-value
(integer) 1
127.0.0.1:6379> HGET myhash field1
"lili"
127.0.0.1:6379> HSET myhash field1 hello field2 world #set多个的key-value
(integer) 1
127.0.0.1:6379> hmget myhash field1 field2 #获取多个字段值
1) "hello"
2) "world"
127.0.0.1:6379> HGETALL myhash #获取hash中全部的值
1) "field1"
2) "hello"
3) "field2"
4) "world"
127.0.0.1:6379> HDEL myhash field1 #删除hash指定key字段,同时对应的value也会删除
(integer) 1
127.0.0.1:6379> HGETALL myhash
1) "field2"
2) "world"
- hlen
127.0.0.1:6379> HGETALL myhash
1) "field2"
2) "world"
3) "field1"
4) "hello"
127.0.0.1:6379> HLEN myhash #获取hash表的字段数量
(integer) 2
127.0.0.1:6379>
- HEXISTS
127.0.0.1:6379> HEXISTS myhash field1 #判断hash中的指定字段是否存在
(integer) 1
127.0.0.1:6379> HEXISTS myhash field3
(integer) 0
- hkeys&hvals
127.0.0.1:6379> hkeys myhash #只获取所有的字段
1) "field2"
2) "field1"
127.0.0.1:6379> hvals myhash #只获取所有的值
1) "world"
2) "hello"
- hincrby
127.0.0.1:6379> HSET myhash field3 5 #指定自增长
(integer) 1
127.0.0.1:6379> hincrby myhash field3 1
(integer) 6
127.0.0.1:6379> hincrby myhash field3 -1
(integer) 5
127.0.0.1:6379> hsetnx myhash field4 hello #如果不存在则可以设置
(integer) 1
127.0.0.1:6379> hsetnx myhash field4 world #如果存在则不可以设置
(integer) 0
127.0.0.1:6379> HSET user:1 name lili
(integer) 1
127.0.0.1:6379> HGET user:1 name
"lili"
标签:127.0,hash,0.1,myhash,field1,Redis,6379,integer
From: https://www.cnblogs.com/OpenSourceSite/p/18230203