七、Hash(哈希)
适合存对象
127.0.0.1:6379> hset myhash field1 xiaoqi #set一个具体的key-value
(integer) 1
127.0.0.1:6379> hget myhash field1 #get一个具体的key-value
"xiaoqi"
127.0.0.1:6379> hmset myhash field2 hello field3 world #set多个值
OK
127.0.0.1:6379> hmget myhash field1 field2 #get多个值
1) "xiaoqi"
2) "hello"
127.0.0.1:6379> hgetall myhash #get全部数据
1) "field1"
2) "xiaoqi"
3) "field2"
4) "hello"
5) "field3"
6) "world"
127.0.0.1:6379> hdel myhash field1 #删除指定的值
(integer) 1
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "hello"
3) "field3"
4) "world"
================================================================================
127.0.0.1:6379> hgetall myhash
1) "field2"
2) "hello"
3) "field3"
4) "world"
127.0.0.1:6379> hlen myhash #获取hash长度
(integer) 2
================================================================================
127.0.0.1:6379> hexists myhash field2 #判断hash中该值是否存在
(integer) 1
127.0.0.1:6379> hexists myhash field5
(integer) 0
================================================================================
127.0.0.1:6379> hkeys myhash #只获得所有的key
1) "field2"
2) "field3"
127.0.0.1:6379> hvals myhash #只获得所有的value
1) "hello"
2) "world"
================================================================================
127.0.0.1:6379> hset myhash field3 5 #指定增量
(integer) 0
127.0.0.1:6379> hset myhash field7 8
(integer) 1
127.0.0.1:6379> HINCRBY myhash field7 2
(integer) 10
127.0.0.1:6379> hsetnx myhash filed5 hello #如果不存在则可以设置
(integer) 1
127.0.0.1:6379> hsetnx myhash filed5 world #如果存在则不能设置
(integer) 0
七、Zset(有序集合)
在set基础上增加了一个值
127.0.0.1:6379> zadd myset 1 one #增加一个值
(integer) 1
127.0.0.1:6379> zadd myset 2 two 3 three #增加多个值
(integer) 2
127.0.0.1:6379> Zrange myset 0 -1 #获取所有值
1) "one"
2) "two"
3) "three"
================================================================================
127.0.0.1:6379> zadd salary 3000 xiaoqi #设置三个用户
(integer) 1
127.0.0.1:6379> zadd salary 2800 xiaowang
(integer) 1
127.0.0.1:6379> zadd salary 600 xiaozhao
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "xiaozhao"
2) "xiaowang"
3) "xiaoqi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf #显示全部的用户,从小到大
1) "xiaozhao"
2) "xiaowang"
3) "xiaoqi"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf withscores #显示全部的用户,从小到大并附带成绩
1) "xiaozhao"
2) "600"
3) "xiaowang"
4) "2800"
5) "xiaoqi"
6) "3000"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 2900 withscores #显示工资少于2900的用户,从小到大并附带成绩
1) "xiaozhao"
2) "600"
3) "xiaowang"
4) "2800"