sets类型及操作
Set是集合,它是string类型的无序集合。set是通过hash
table实现的,添加,删除和查找复杂度都是0(1)。对集合我们可以取并集、交集、差集。通过这些操作我们可以实现sns中的好友推荐和blog的tag功能。
sadd:向集合中添加一个元素,通名称为key的set中添加元素。
[[email protected] bin]$ ./redis-cli 127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 0 127.0.0.1:6379> |
通过smembers查看集合中有哪些元素
127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> |
srem:删除名称为key的set中的元素,删除成功返回1,删除失败返回0
127.0.0.1:6379> smembers myset2 1) "two" 2) "one" 3) "three" 127.0.0.1:6379> srem myset2 two (integer) 1 127.0.0.1:6379> srem myset2 two (integer) 0 127.0.0.1:6379> smembers myset2 1) "one" 2) "three" 127.0.0.1:6379> |
spop:无法指定弹出元素,是随机返回并删除名称为key的set中一个元素。
127.0.0.1:6379> sadd myset3 one (integer) 1 127.0.0.1:6379> sadd myset3 two (integer) 1 127.0.0.1:6379> sadd myset3 three (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> smembers myset3 1) "four" 2) "three" 3) "one" 4) "two" 5) "five" 127.0.0.1:6379> spop myset3 "three" 127.0.0.1:6379> smembers myset3 1) "one" 2) "four" 3) "five" 4) "two" 127.0.0.1:6379> spop myset3 "four" 127.0.0.1:6379> smembers myset3 1) "one" 2) "five" 3) "two" 127.0.0.1:6379> |
sdiff:两个集合的差集。
返回所有给定key与第一个key的差集。
127.0.0.1:6379> smembers myset3 1) "one" 2) "four" 3) "five" 4) "two" 127.0.0.1:6379> spop myset3 "four" 127.0.0.1:6379> smembers myset3 1) "one" 2) "five" 3) "two" 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sadd myset2 one (integer) 1 127.0.0.1:6379> smembers myset2 1) "one" 2) "four" 3) "three" 127.0.0.1:6379> sdiff myset2 myset3 1) "three" 2) "four" 127.0.0.1:6379> |
sdiffstore:返回所有给定key与第一个key的差集,并将结果存为另一个key.
sdiffstore myset4 myset2 myset3表示的意思是将myset3和myset2的差集存储到myset4里面。
127.0.0.1:6379> sadd myset2 one (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset3 one (integer) 1 127.0.0.1:6379> sadd myset3 two (integer) 1 127.0.0.1:6379> sadd myset3 three (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> sadd myset3 six (integer) 1 127.0.0.1:6379> sdiff myset2 myset3 (empty list or set) 127.0.0.1:6379> sdiff myset3 myset2 1) "six" 2) "five" 3) "four" 127.0.0.1:6379> sdiffstore myset4 myset3 myset2将myset3和myset2的差集存入myset4中 (integer) 3 127.0.0.1:6379> smembers myset4 1) "six" 2) "five" 3) "four" 127.0.0.1:6379> |
sinter:
返回所有给定key的交集
sinterstore将取出的差集存入到另外的一个集合中。
127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sadd myset2 five (integer) 1 127.0.0.1:6379> sadd myset3 four (integer) 1 127.0.0.1:6379> sadd myset3 five (integer) 1 127.0.0.1:6379> sadd myset3 six (integer) 1 127.0.0.1:6379> sadd myset3 seven (integer) 1 127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "four" 2) "three" 3) "five" 4) "two" 127.0.0.1:6379> smembers myset3 1) "seven" 2) "four" 3) "six" 4) "five" 127.0.0.1:6379> sinter myset3 myset2 1) "four" 2) "five" 127.0.0.1:6379> sinterstore myset6 myset3 myset2 (integer) 2 127.0.0.1:6379> smembers myset6 1) "four" 2) "five" 127.0.0.1:6379> |
sunion:表示插入并集。
返回所有给定key的并集。
127.0.0.1:6379> sadd myset1 one (integer) 1 127.0.0.1:6379> sadd myset2 two (integer) 1 127.0.0.1:6379> sadd myset1 two (integer) 1 127.0.0.1:6379> sadd myset2 three (integer) 1 127.0.0.1:6379> sadd myset2 four (integer) 1 127.0.0.1:6379> sunion myset1 myset2 1) "three" 2) "two" 3) "four" 4) "one" 127.0.0.1:6379> (integer) 4 127.0.0.1:6379> 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> smembers myset7 1) "three" 2) "two" 3) "four" 4) "one" 127.0.0.1:6379> |
smove:从第一个key对应的set中移除member并添加到第二个对应的set中。(将第一个集合中的元素剪切下来放入第二个元素中)
将smove myset2 myset7 three表示:myset2中的three元素扔到myset7中
127.0.0.1:6379> smembers myset1 1) "two" 2) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> sadd myset1 five (integer) 1 127.0.0.1:6379> smembers myset8 (empty list or set) 127.0.0.1:6379> smove myset1 myset8 two (integer) 1 127.0.0.1:6379> smembers myset8 1) "two" 127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> |
scard返回名称为key的set的元素个数。(看集合中的元素个数)
127.0.0.1:6379> smembers myset1 看myset1集合中的元素有哪些 1) "five" 2) "one" 127.0.0.1:6379> smembers myset2 1) "three" 2) "two" 3) "four" 127.0.0.1:6379> smembers myset8 1) "two" 127.0.0.1:6379> scard myset1 (integer) 2 127.0.0.1:6379> scard myset2 (integer) 3 127.0.0.1:6379> scard myset8 (integer) 1 127.0.0.1:6379> |
sismember:测试member是否是名称为key的set的元素。(判断某个元素是否是某个集合中的元素,是返回1,不是返回0)
127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> sismember myset1 one (integer) 1 127.0.0.1:6379> sismember myset1 two (integer) 0 127.0.0.1:6379> |
srandmember:随机返回名称为key的set的一个元素,但不删除元素。
127.0.0.1:6379> smembers myset1 1) "five" 2) "one" 127.0.0.1:6379> srandmember myset7 "three" 127.0.0.1:6379> srandmember myset7 "two" 127.0.0.1:6379> srandmember myset7 "four" 127.0.0.1:6379> srandmember myset7 "three" |
Sorted sets类型及操作
Sortedset是set的一个升级版本,它在set的基础上增加了一个顺序属性,这一属性在添加修改元素的时候可以指定,每次指定后,zset会自动重新按新的值调整顺序。可以理解为由两列的mysql表,一列存value,一列存顺序。操作中key理解为zset的名字。
zadd:向名称为key的zset中添加元素member,score用于排序。如果该元素存在,则更新器顺序。语法:zadd
set名称
顺序
值
127.0.0.1:6379> zadd myzset 1 "one" (integer) 1 127.0.0.1:6379> zadd myzset 2 "two" (integer) 1 127.0.0.1:6379> zadd myzset 3 "two" (integer) 0 这里的0 -1表示顺序号 127.0.0.1:6379> zrange myzset 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> 再如: 127.0.0.1:6379> zadd sset1 1 two (integer) 1 127.0.0.1:6379> zadd sset1 2 two (integer) 0 127.0.0.1:6379> zadd sset1 3 two (integer) 0 127.0.0.1:6379> zadd sset1 1 one (integer) 1 127.0.0.1:6379> zrange sset1 0 -1 1) "one" 2) "two" 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> |
zrem:删除名称为key的zset中的元素member
127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "3" 127.0.0.1:6379> zrem sset1 two (integer) 1 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> |
zincrby:如果在名称为key的zset中已经存在元素member,则该元素的score增加increment,否则向该集合中添加该元素,其score的值为increment。
127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> zincrby sset1 2 one "3" 127.0.0.1:6379> zrange sset1 0 -1 withscores 1) "one" 2) "3" 127.0.0.1:6379> |
zrank:返回名称为key的zset中的member元素的排名。(按score从小到大排序)即下标。(返回的是索引值,和序号值是有差别的)
127.0.0.1:6379> zadd sset2 1 one (integer) 1 127.0.0.1:6379> zadd sset2 2 two (integer) 1 127.0.0.1:6379> zadd sset2 3 three (integer) 1 127.0.0.1:6379> zadd sset2 4 four (integer) 1 127.0.0.1:6379> zadd sset2 5 five (integer) 1 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5" 127.0.0.1:6379> zrank sset2 four (integer) 3 127.0.0.1:6379> |
zrevrank:返回名称为key的zset中member元素的排名。(按score从大到小排序)即下标。
127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5" 127.0.0.1:6379> zrevrank sset2 four (integer) 1 127.0.0.1:6379> zrevrank sset2 two (integer) 3 |
zrevrange:返回名称为key的zset(按score从大到小顺序)中的index从start到end的所有元素。(也就是说是反向排序)
127.0.0.1:6379> zrevrange sset2 0 -1 withscores 1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1" 127.0.0.1:6379> |
zrangebyscore:找到序号在指定范围内的元素
127.0.0.1:6379> zrevrange sset2 0 -1 withscores 1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1" 127.0.0.1:6379> zrangebyscore sset2 2 4 withscores 1) "two" 2) "2" 3) "three" 4) "3" 5) "four" 6) "4" 127.0.0.1:6379> |
zcount:返回集合中score在给定区间的数量。
127.0.0.1:6379> zrevrange sset2 0 -1 withscores 1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1" 127.0.0.1:6379> zcount sset2 2 4 (integer) 3 127.0.0.1:6379> zcount sset2 4 5 (integer) 2 127.0.0.1:6379> |
zcard:返回集合中的所有元素的个数
127.0.0.1:6379> zrevrange sset2 0 -1 withscores 1) "five" 2) "5" 3) "four" 4) "4" 5) "three" 6) "3" 7) "two" 8) "2" 9) "one" 10) "1" 127.0.0.1:6379> zcard sset2 (integer) 5 127.0.0.1:6379> |
zremrangebyrank:删除集合中排名在给定区间的元素(按照索引来删除)。
127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3" 7) "four" 8) "4" 9) "five" 10) "5" 127.0.0.1:6379> zremrangebyrank sset2 2 3 (integer) 2 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> |
zremrangebyscore:删除集合中score在给定区间的元素(表示的是按照顺序删除)。
127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> zremrangebyscore sset2 3 4 (integer) 0 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "five" 6) "5" 127.0.0.1:6379> zremrangebyscore sset2 2 5 (integer) 2 127.0.0.1:6379> zrange sset2 0 -1 withscores 1) "one" 2) "1" 127.0.0.1:6379> |