sets类型
sets类型以及操作Set是无序集合,它是string类型的无序集合。set是通过hash table实现的,添加、删除和查找的复杂度都是0(1)。对集合我们可以取并集、交集、差集。通过这些操作我们可以实现sns中的好友推荐和blog的tag功能。sadd向名称为key的set中添加元素,返回添加成功的个数127.0.0.1:6379> sadd key member [member ...]127.0.0.1:6379> sadd myset1 one(integer) 1127.0.0.1:6379> sadd myset1 two three(integer) 2
smembers查看集合元素
127.0.0.1:6379> smembers key127.0.0.1:6379> smembers myset11) "one"2) "two"3) "three"
srem删除名称为key的set中元素,返回成功执行的个数
127.0.0.1:6379> srem key member [member ...]127.0.0.1:6379> srem myset1 one(integer) 1127.0.0.1:6379> smembers myset11) "two"2) "three"
spop随机返回并删除名称为key的set中的一个元素
spop key [count]127.0.0.1:6379> spop myset1 21) "two"2) "three"127.0.0.1:6379> smembers myset1(empty list or set)
sdiff返回所有给定key与第一个key的差集谁在前面以谁为标准,返回其他集合少于标准的
127.0.0.1:6379> sdiff key [key ...]127.0.0.1:6379> smembers myset21) "two"2) "four"3) "three"127.0.0.1:6379> smembers myset11) "one"2) "two"3) "three"127.0.0.1:6379> sdiff myset1 myset21) "one"127.0.0.1:6379> sdiff myset2 myset11) "four"
sdiffstore返回两个集合的差集,并将差集存储到另一个集合里面
127.0.0.1:6379> sdiffstore destination key [key ...]127.0.0.1:6379> sdiffstore myset3 myset1(integer) 3127.0.0.1:6379> smembers myset31) "one"2) "two"3) "three"127.0.0.1:6379> sdiff myset4 myset1 myset2(empty list or set)127.0.0.1:6379> smembers myset11) "one"2) "two"3) "three"127.0.0.1:6379> smembers myset21) "two"2) "four"3) "three"127.0.0.1:6379> sdiffstore myset4 myset1 myset2(integer) 1127.0.0.1:6379> smembers myset41) "one"
sinter返回所有给定key的交集
127.0.0.1:6379> sinter key [key ...]127.0.0.1:6379> smembers myset11) "one"2) "two"3) "three"127.0.0.1:6379> smembers myset21) "two"2) "four"3) "three"127.0.0.1:6379> sinter myset1 myset21) "two"2) "three"
sinterstore返回两个集合的交集并且储存在指定集合内
127.0.0.1:6379> sinterstore destination key [key ...]127.0.0.1:6379> sinterstore myset4 myset1 myset2(integer) 2127.0.0.1:6379> smembers myset41) "two"2) "three"
sunion返回所有给定集合的并集
127.0.0.1:6379> sunion key [key ...]127.0.0.1:6379> smembers myset11) "one"2) "two"3) "three"127.0.0.1:6379> smembers myset21) "two"2) "four"3) "three"127.0.0.1:6379> sunion myset1 myset21) "one"2) "two"3) "four"4) "three"
sunionstrore返回给顶集合的并集,并且存储在另一个集合
127.0.0.1:6379> sunionstore destination key [key ...]127.0.0.1:6379> sunionstore myset5 myset1 myset2(integer) 4127.0.0.1:6379> smembers myset51) "one"2) "two"3) "four"4) "three"
smove从个一的个key中的对应的set中移除member并添加到第二个对应的set中
127.0.0.1:6379> smove source destination member127.0.0.1:6379> smembers myset11) "one"2) "two"127.0.0.1:6379> smembers myset21) "two"2) "four"3) "three"127.0.0.1:6379> smove myset1 myset2 one(integer) 1127.0.0.1:6379> smembers myset11) "two"127.0.0.1:6379> smembers myset21) "one"2) "two"3) "four"4) "three"
scard返回名称为key的集合中的集合的个数
127.0.0.1:6379> scard key127.0.0.1:6379> scard myset1(integer) 1127.0.0.1:6379> scard myset2(integer) 4
sismember测试某个元素是不是某个集合的元素
127.0.0.1:6379> sismember key member127.0.0.1:6379> sismember myset1 one(integer) 0127.0.0.1:6379> sismember myset two(integer) 0127.0.0.1:6379> smembers myset11) "two"127.0.0.1:6379> sismember myset1 two(integer) 1
srandmember随机返回集合中的一个元素,但不删除
127.0.0.1:6379> srandmember key [count]127.0.0.1:6379> srandmember myset2"four"127.0.0.1:6379> srandmember myset2"one"127.0.0.1:6379> srandmember myset2 61) "one"2) "two"3) "three"4) "four"