本节内容:
redis数据库的set类型与zset类型
4.sets类型
Set是集合,它是string类型的无序集合。set是通过hash table实现的,添加、删除和查找的复杂度都是O(1)。对集合我们可以取并集、交集、差集。
通过这些操作可以实现sns中的好友推荐和blog的tag功能。
sadd:向名称为key的set中添加元素。
redis 127.0.0.1:6379> sadd myset "hello"
(integer) 1 //表示添加成功
redis 127.0.0.1:6379> sadd myset "world"
(integer) 1 //表示添加成功
redis 127.0.0.1:6379> sadd myset "world"
(integer) 0 //表示添加不成功
redis 127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
srem:删除名称为key的set中的元素。
redis 127.0.0.1:6379> sadd myset2 one
(integer) 1
redis 127.0.0.1:6379> sadd myset2 two
(integer) 1
redis 127.0.0.1:6379> sadd myset2 three
(integer) 1
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
3) "one"
redis 127.0.0.1:6379> srem myset2 two
(integer) 1
redis 127.0.0.1:6379> srem myset2 two
(integer) 0
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "one"
spop:随机返回并删除名称为key的set中一个元素。
redis 127.0.0.1:6379> sadd myset3 one
(integer) 1
redis 127.0.0.1:6379> sadd myset3 two
(integer) 1
redis 127.0.0.1:6379> sadd myset3 three
(integer) 1
redis 127.0.0.1:6379> sadd myset3 four
(integer) 1
redis 127.0.0.1:6379> sadd myset3 five
(integer) 1
redis 127.0.0.1:6379> smembers myset3
1) "four"
2) "three"
3) "two"
4) "one"
5) "five"
redis 127.0.0.1:6379> spop myset3
"one"
redis 127.0.0.1:6379> spop myset3
"four"
redis 127.0.0.1:6379> spop myset3
"three"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "five"
sdiff:返回所有给定key与第一个key的差集。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sdiff myset2 myset3
1) "three"
sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sdiffstore myset4 myset2 myset3
(integer) 1
redis 127.0.0.1:6379> smembers myset4
1) "three"
sinter:返回所有给定key的交集。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sinter myset2 myset3
1) "two"
sinterstore:返回所有给定key的交集,并将结果存为另一个key。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sinterstore myset6 myset2 myset3
(integer) 1
redis 127.0.0.1:6379> smembers myset6
1) "two"
sunion:返回所有给定key的并集。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sunion myset2 myset3
1) "three"
2) "one"
3) "two"
sunionstore:返回所有给定key的并集。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> sunionstore myset7 myset2 myset3
(integer) 3
redis 127.0.0.1:6379> smembers myset7
1) "three"
2) "one"
3) "two"
smove:从第一个key对应的set中移除member并添加到第二个对应的set中。
redis 127.0.0.1:6379> smembers myset2
1) "three"
2) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> smove myset2 myset7 three
(integer) 1
redis 127.0.0.1:6379> smembers myset2
1) "two"
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
3) "three"
scard:返回名称为key的set的元素个数。
redis 127.0.0.1:6379> scard myset3
(integer) 3
sismember:测试member是否是名称为key的set的元素。
redis 127.0.0.1:6379> smembers myset2
1) "two"
redis 127.0.0.1:6379> sismember myset2 two
(integer) 1
redis 127.0.0.1:6379> sismember myset2 one
(integer) 0
srandmember:随机返回名称为key的set的一个元素,但不删除元素。
redis 127.0.0.1:6379> smembers myset3
1) "two"
2) "one"
redis 127.0.0.1:6379> srandmember myset3
"two"
redis 127.0.0.1:6379> srandmember myset3
"one"
5.sorted sets类型
sorted set是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解为有两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。
zadd:向名称为key的zset中添加元素member,score用于排序。如果该元素存在,则更新其顺序。
redis 127.0.0.1:6379> zadd myzset 1 "one"
(integer) 1
redis 127.0.0.1:6379> zadd myzset 2 "two"
(integer) 1
redis 127.0.0.1:6379> zadd myzset 3 "two"
(integer) 0
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
zrem:删除名称为key的zset中的元素member
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "3"
redis 127.0.0.1:6379> zrem myzset two
(integer) 1
redis 127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
zincrby:如果在名称为key的zset中已经存在元素member,则该元素的score增加increment,否则向该集合中添加该元素,其score的值为increment。
redis 127.0.0.1:6379> zadd myzset2 1 "one"
(integer) 1
redis 127.0.0.1:6379> zadd myzset2 2 "two"
(integer) 1
redis 127.0.0.1:6379> zincrby myzset2 2 "one"
"3" //代表one的顺序号变成了3
redis 127.0.0.1:6379> zrange myzset2 0 -1 withscores
1) "two"
2) "2"
3) "one"
4) "3"
zrank:返回名称为key的zset中member元素的排名(按score从小到大排序)即下标。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
redis 127.0.0.1:6379> zrank myzset3 two
(integer) 1 //two的下标是1(从0开始)
zrevrank:返回名称为key的zset中member元素的排名(按score从大到小排序)即下标。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
redis 127.0.0.1:6379> zrevrank myzset3 two
(integer) 2
zrevrange:返回名称为key的zset(按score从大到小顺序)中的index从start到end的所有元素。
redis 127.0.0.1:6379> zrevrange myzset3 0 -1 withscores
1) "five"
2) "5"
3) "three"
4) "3"
5) "two"
6) "2"
7) "one"
8) "1"
redis 127.0.0.1:6379>
zrangebyscore:返回集合中score在给定区间的元素。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis 127.0.0.1:6379> zrangebyscore myzset3 2 3 withscores
1) "two"
2) "2"
3) "three"
4) "3"
zcount:返回集合中score在给定区间的数量。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
redis 127.0.0.1:6379> zcount myzset3 2 3
(integer) 2
zcard:返回集合中的元素个数。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
7) "five"
8) "5"
redis 127.0.0.1:6379> zcard myzset3
(integer) 4
zremrangebyrank:删除集合中排名在给定区间的元素。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
redis 127.0.0.1:6379> zremrangebyrank myzset3 1 1
(integer) 1 //只删除一个
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
zremrangebyscore:删除集合中score在给定区间的元素。
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "two"
4) "2"
5) "three"
6) "3"
redis 127.0.0.1:6379> zremrangebyscore myzset3 1 2
(integer) 2 //删除顺序为1和2的
redis 127.0.0.1:6379> zrange myzset3 0 -1 withscores
1) "three"
2) "3"