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