1、sadd
sadd key member1 member2...
- 将member1...加入到集合中,已存在的会被忽略
- 返回值为加入的新元素数量
127.0.0.1:6379> sadd test 1 1 2
(integer) 2
2、sismember
sismember key member
- member是否是key的成员
- 是则返回1,否则返回0
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> sismember test 2
(integer) 1
127.0.0.1:6379> sismember test 10
(integer) 0
3、spop
spop key
- 移除并返回集合中的一个随机元素
- 返回值是被移除的元素
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> spop test
"2"
127.0.0.1:6379> spop test
"3"
127.0.0.1:6379> spop test
"1"
127.0.0.1:6379> spop test
(nil)
4、srandmember
srandmember key [count]
- 只有key参数,返回集合中一个随机元素
- count为正数且小于集合大小,返回一个count个元素的数组,数组中的元素各不相同
- count为整数且大于等于集合大小,返回整个集合
- count为负数,返回一个|count|大小的数组,数组中的元素可能出现多次
- 与spop的不同是不会删除元素
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> srandmember test
"2"
127.0.0.1:6379> srandmember test 2 //正数
1) "1"
2) "2"
127.0.0.1:6379> srandmember test 5 //正数大于集合大小,返回整个集合
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> srandmember test -5 //负数,返回5个且可能重复
1) "2"
2) "2"
3) "2"
4) "3"
5) "2"
5、srem
srem key member1 member2 ...
- 集合key中删除成员member1,member2...,不存在的忽略
- 返回成功删除的数量
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> srem test 1 2 10
(integer) 2
6、smove
smove source destination member
- member元素从source集合移动到destination集合
- 原子操作
- source集合不包含member元素,返回0,member元素被成功移除返回1
127.0.0.1:6379> sadd test1 1 2 3
(integer) 3
127.0.0.1:6379> sadd test2 4 5
(integer) 2
127.0.0.1:6379> smove test1 test2 10 //10不在test1,返回0
(integer) 0
127.0.0.1:6379> smove test1 test2 3
(integer) 1
127.0.0.1:6379> sadd test1 3
(integer) 1
127.0.0.1:6379> smove test1 test2 3 //3在test1中也在test2中
(integer) 1
7、scard
scard key
- 集合key中元素数量
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> scard test
(integer) 3
127.0.0.1:6379> scard no_exsits //不存在的key,返回0
(integer) 0
8、smembers
smembers key
- 集合key中的所有成员,不存在的key视为空集
127.0.0.1:6379> sadd test 1 2 3
(integer) 3
127.0.0.1:6379> smembers test
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers no_exists
(empty list or set)
9、sscan
SSCAN key cursor [MATCH pattern] [COUNT count]
10、sinter
sinter key1 key2 ...
- key1,key2...的交集
127.0.0.1:6379> sadd test1 1 2 3
(integer) 3
127.0.0.1:6379> sadd test2 1 2
(integer) 2
127.0.0.1:6379> sinter test1 test2
1) "1"
2) "2"
127.0.0.1:6379> sinter test1 test2 no_exists //no_exists是不存在的集合
(empty list or set)
11、sinterstore
sinter destination key1 key2...
- 将key1,key2...的交集结果存到destination中
- 返回值为结果集的元素数目
127.0.0.1:6379> sadd test1 1 2 3
(integer) 3
127.0.0.1:6379> sadd test2 1 2
(integer) 2
127.0.0.1:6379> sinterstore test3 test1 test2
(integer) 2
127.0.0.1:6379> smembers test3
1) "1"
2) "2"
12、sunion
sunion key1 key2...
- 返回key1,key2...的并集
127.0.0.1:6379> sadd test1 1 2 3
(integer) 3
127.0.0.1:6379> sadd test2 2 3 4 5
(integer) 4
127.0.0.1:6379> sunion test1 test2
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
13、sunionstore
sunionstore destination key1 key2...
- key1,key2...的并集存到destination中
- 返回值为结果集的元素数目
127.0.0.1:6379> sadd test1 1 2 3
(integer) 3
127.0.0.1:6379> sadd test2 2 3 4 5
(integer) 4
127.0.0.1:6379> sunionstore test3 test1 test2
(integer) 5
14、sdiff
sdiff key1 key2...
- 返回key1 key2...的差集
127.0.0.1:6379> smembers test1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers test2
1) "2"
2) "3"
127.0.0.1:6379> sdiff test1 test2
1) "1"
15、sdiffstore
sdiffstore destination key1,key2...
- key1,key2...的差集结果存到destination中
127.0.0.1:6379> smembers test1
1) "1"
2) "2"
3) "3"
127.0.0.1:6379> smembers test2
1) "2"
2) "3"
127.0.0.1:6379> sdiffstore test3 test1 test2
(integer) 1
127.0.0.1:6379> smembers test3
1) "1"