hash集合
hash类似于map集合。存在键值对。
1.存值于取值:hset 于hget
127.0.0.1:6379> hset myhash f1 liuqing #设置一个hash集合为myhash 其中key为f1 值为liuqing
(integer) 1
127.0.0.1:6379> hset myhash f2 love
(integer) 1
127.0.0.1:6379> hget myhash f1 #获取hash集合key为f1的值
"liuqing"
2.多个存值与取值:hmget 与hmset
127.0.0.1:6379> hmset myhash f3 you f4 liuqing
OK
127.0.0.1:6379> hmget myhash f1 f2 f3 f4
1) "liuqing"
2) "love"
3) "you"
4) "liuqing"
3。获取集合中全部的键与值:hgetall
127.0.0.1:6379> hgetall myhash
1) "f1"
2) "liuqing"
3) "f2"
4) "love"
5) "f3"
6) "you"
7) "f4"
8) "liuqing"
4.移除一个值与查看集合长度:hdel 与 hlen
127.0.0.1:6379> hdel myhash f1
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "f2"
2) "love"
3) "f3"
4) "you"
5) "f4"
6) "liuqing"
127.0.0.1:6379> hlen myhash
(integer) 3
5.判断是否存在有一个key:hexists
127.0.0.1:6379> Hexists myhash f1
(integer) 0
127.0.0.1:6379> Hexists myhash f2
(integer) 1
6.获取全部的key 或者 value:hkeys 与hvals
127.0.0.1:6379> hkeys myhash
1) "f2"
2) "f3"
3) "f4"
127.0.0.1:6379> hvals myhash
1) "love"
2) "you"
3) "liuqing"
7.自增 ,不可覆盖存值:Hincrby yv hsetnx
127.0.0.1:6379> hset myhash f1 5
(integer) 1
127.0.0.1:6379> HINCRBY myhash f1 1
(integer) 6
127.0.0.1:6379> HINCRBY myhash f1 -1
(integer) 5
127.0.0.1:6379> clear
127.0.0.1:6379> hsetnx myhash f5 aaa #不存在f5,成功
(integer) 1
127.0.0.1:6379> hsetnx myhash f5 bbb #存在 ,失败
(integer) 0
127.0.0.1:6379>