SADD:向集合中添加元素
语法:SADD key member [...]
SADD web www.baidu.com
SADD web www.sina.com.cn www.amazon.com
SADD web www.taobao.com www.bilibili.com
127.0.0.1:6379> SADD web www.baidu.com (integer) 1 127.0.0.1:6379> SADD web www.sina.com.cn www.amazon.com (integer) 2 127.0.0.1:6379> SADD web www.taobao.com www.bilibili.com (integer) 2 127.0.0.1:6379> SMEMBERS web 1) "www.amazon.com" 2) "www.sina.com.cn" 3) "www.baidu.com" 4) "www.taobao.com" 5) "www.bilibili.com"
SMEMBERS:返回指定集合中的元素
语法:SMEMBERS key
SMEMBERS web
SISMEMBER:检测 member 是否是集合中的成员
语法:SISMEMBER key member
SISMEMBER web www.baidu.com
SISMEMBER web www.google.com #不存在返回 0 #
127.0.0.1:6379> SISMEMBER web www.baidu.com (integer) 1 127.0.0.1:6379> SISMEMBER web www.google.com (integer) 0
SREM:删除集合中的一个或者多个成员
语法:SREM key member [member...]
SREM web www.baidu.com
SREM web www.taobao.com www.bilibili.com #当同时删除多个 member,有不存在的或删除失败的 member 时,返回值时删除成功的个数#
127.0.0.1:6379> SMEMBERS web 1) "www.baidu.com" 2) "www.taobao.com" 3) "www.sina.com.cn" 4) "www.bilibili.com" 5) "www.amazon.com" 127.0.0.1:6379> SREM web www.taobao.com www.baidu.com (integer) 2 127.0.0.1:6379> SREM web www.taobao.com www.bilibili.com (integer) 1
SPOP:删除并返回集合中的一个随机元素
语法:SPOP key
SADD course php java ios android c c++ js
SPOP course
SMEMBERS course
127.0.0.1:6379> SADD course php java ios android c c++ js (integer) 7 127.0.0.1:6379> SPOP course "php" 127.0.0.1:6379> SMEMBERS course 1) "c" 2) "ios" 3) "android" 4) "c++" 5) "java" 6) "js"
SRANDMEMBER:随机返回集合中的元素
语法:SRANDMEMBER key count
# count 如果小于集合中的元素,返回 count 个随机元素的集合数组;如果 count 大于集合中元素的个数,这时候会返回整个集合,如果 count 为负数,返回数组的长度是 count 的绝对值,数组中的成员可能出现重复#
SRANDMEMBER course
SRANDMEMBER course 2
SRANDMEMBER course 50
SRANDMEMBER course -2
127.0.0.1:6379> SRANDMEMBER course "ios" 127.0.0.1:6379> SRANDMEMBER course 2 1) "c" 2) "java" 127.0.0.1:6379> SRANDMEMBER course 50 1) "ios" 2) "c++" 3) "android" 4) "c" 5) "js" 6) "java" 127.0.0.1:6379> SRANDMEMBER course -2 1) "ios" 2) "js"
SDIFF:返回集合间的差集
语法:SDIFF key key ...
SADD testSet1 a b c d e
SADD testSet2 b c d e f
SDIFF testSet1 testSet2 #在集合 1 中,不在集合 2 中的元素#
SDIFF testSet2 testSet1 #在集合 2 中,不在集合 1 中的元素#
SADD testSet3 x y z
SDIFF testSet1 testSet2 testSet3 #在两个以上的集合中取差集的时候,先对前两个进行取值,所得到的结果再与后面的集合进行对比,当差集为空的时候回报错#
127.0.0.1:6379> SADD testSet1 a b c d e (integer) 5 127.0.0.1:6379> SADD testSet2 b c d e f (integer) 5 127.0.0.1:6379> SDIFF testSet1 testSet2 1) "a" 127.0.0.1:6379> SDIFF testSet2 testSet1 1) "f" 127.0.0.1:6379> SADD testSet3 x y z (integer) 4 127.0.0.1:6379> SDIFF testSet1 testSet2 testSet3 1) "a" 127.0.0.1:6379> SADD testSet3 x y z a (integer) 4 127.0.0.1:6379> SDIFF testSet1 testSet2 testSet3 (empty list or set)
SINTER:返回集合间的交集
语法:SINTER key key...
SINTER testSet1 testSet2
SINTER testSet1 testSet2 testSet3
SADD testSet3 b
127.0.0.1:6379> SINTER testSet1 testSet2 1) "c" 2) "b" 3) "d" 4) "e" 127.0.0.1:6379> SINTER testSet1 testSet2 testSet3 (empty list or set) 127.0.0.1:6379> SADD testSet3 b (integer) 1 127.0.0.1:6379> SINTER testSet1 testSet2 testSet3 1) "b"
SUNION:返回集合中并集
语法:SUNION key key...
SUNION testSet1 testSet2
SUNION testSet1 testSet2 testSet3
127.0.0.1:6379> SUNION testSet1 testSet2 1) "a" 2) "c" 3) "f" 4) "b" 5) "d" 6) "e" 127.0.0.1:6379> SUNION testSet1 testSet2 testSet3 1) "a" 2) "c" 3) "x" 4) "f" 5) "b" 6) "d" 7) "z" 8) "e" 9) "y"
SCARD:返回集合中元素个数
语法:SCARD key
SCARD testSet1
127.0.0.1:6379> SCARD testSet1 (integer) 5
SDIFFSTORE:将差集结果保存到指定集合中
语法:SDIFFSTORE destination key key ...
SDIFFSTORE diffSet testSet1 testSet2
SMEMBERS diffSet #返回值为集合 1 和 2 的差集 a #
SADD diffSet b #再添加一个元素到 diffSet 里,这是 diffSet 里有两个元素了#
SMEMBERS diffSet
SDIFFSTORE diffSet testSet2 testSet1
SMEMBERS diffSet #原本 diffSet 里原有的值被清空了,重新写入新值 f #
127.0.0.1:6379> SDIFFSTORE diffSet testSet1 testSet2 (integer) 1 127.0.0.1:6379> SMEMBERS diffSet 1) "a" 127.0.0.1:6379> SADD diffSet b (integer) 1 127.0.0.1:6379> SMEMBERS diffSet 1) "a" 2) "b" 127.0.0.1:6379> SDIFFSTORE diffSet testSet2 testSet1 (integer) 1 127.0.0.1:6379> SMEMBERS diffSet 1) "f"
SINTERSTORE:将交集的结果保存在指定集合中
语法:SINTERSTORE destination key key ...
SUNIONSTORE:将并集的结果保存到指定集合中
语法:SUNIONSTORE destination key key ...
SMOVE:将集合中元素移动另外一个集合中
语法:SMOVE source destination member #原子操作#
SMOVE testSet2 testSet1 f
SADD testSet2 f
SMOVE testSet2 testSet1 f
SMEMBERS testSet2
127.0.0.1:6379> SMEMBERS testSet1 1) "a" 127.0.0.1:6379> SMEMBERS testSet2 1) "c" 2) "f" 3) "b" 4) "d" 5) "e" 127.0.0.1:6379> SMOVE testSet2 testSet1 f (integer) 1 127.0.0.1:6379> SMEMBERS testSet2 1) "c" 2) "b" 3) "d" 4) "e" 127.0.0.1:6379> SMEMBERS testSet1 1) "a" 2) "f"