1、将1个或者多个元素添加到集合:SADD key value [value ...] ;如果key不存在就创建一个,如果value存在就不忽略; 如果key不是集合就返回错误;
2、显示集合里面的所有元素:SMEMBERS key
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> SADD test abc def hij 123 234
(integer) 5
127.0.0.1:6379> SMEMBERS test
1) "123"
2) "def"
3) "abc"
4) "hij"
5) "234"
3、移除并返回set里面的一个元素:SPOP key
127.0.0.1:6379> SMEMBERS test
1) "123"
2) "def"
3) "abc"
4) "hij"
5) "234"
127.0.0.1:6379> SPOP test
"hij"
127.0.0.1:6379> SMEMBERS test
1) "234"
2) "abc"
3) "123"
4) "def"
127.0.0.1:6379> SPOP test
"abc"
127.0.0.1:6379> SMEMBERS test
1) "234"
2) "123"
3) "def"
4、从set里面移除指定一个元素或者多个元素:SREM key value [value ...] ;如果value不存在就忽略
127.0.0.1:6379> SREM test 234
(integer) 1
127.0.0.1:6379> SMEMBERS test
1) "123"
2) "def"
127.0.0.1:6379> SREM test 123 def hij
(integer) 2
127.0.0.1:6379> SMEMBERS test
(empty list or set)
127.0.0.1:6379>
5、将source集合member移动到dest集合里面 : SMOVE source dest member ;如果source不存在或者不包含指定元素,smove不执行任何操作;如果dest已经包含了member元素,那么SMOVE只是简单的执行source删除操作;smove是一个原子操作;
127.0.0.1:6379> SMEMBERS test
1) "444"
2) "abc"
3) "apple"
4) "123"
5) "555"
6) "xiaoming"
127.0.0.1:6379> SMOVE test newtest 444
(integer) 1
127.0.0.1:6379> SMEMBERS newtest
1) "444"
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379>
6、随机返回集合一个元素:SRANDMEMBER key [count] ;和spop不一样,srandmember不删除源set里面的成员;
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SRANDMEMBER test
"xiaoming"
127.0.0.1:6379> SRANDMEMBER test
"abc"
127.0.0.1:6379> SRANDMEMBER test
"555"
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
如果count大于0,小于集合基数,就返回给定count个数的数组,元素各不相同;如果大于基数就返回所有的元素;
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SRANDMEMBER test 3
1) "abc"
2) "123"
3) "555"
127.0.0.1:6379> SRANDMEMBER test 3
1) "abc"
2) "apple"
3) "xiaoming"
127.0.0.1:6379> SRANDMEMBER test 1100
1) "555"
2) "123"
3) "apple"
4) "abc"
5) "xiaoming"
如果count小于0,那么返回一个数组,元素可能相同,长度为count的绝对值;
127.0.0.1:6379> SRANDMEMBER test 1100
1) "555"
2) "123"
3) "apple"
4) "abc"
5) "xiaoming"
127.0.0.1:6379> SRANDMEMBER test -3
1) "xiaoming"
2) "555"
3) "xiaoming"
7、返回一个集合的全部成员,取并集:SUNION key [key ...]
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "444"
127.0.0.1:6379> SUNION test newtest
1) "apple"
2) "abc"
3) "444"
4) "123"
5) "555"
6) "xiaoming"
8、和SUNION类适,但是SUNIONSTORE 还要存储到一个dest集合里面: SUNIONSTORE key [key ...]
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "444"
127.0.0.1:6379> SUNIONSTORE mytest test newtest
(integer) 6
127.0.0.1:6379> SMEMBERS mytest
1) "apple"
2) "abc"
3) "444"
4) "123"
5) "555"
6) "xiaoming"
127.0.0.1:6379>
9、返回集合的个数:SCARD key
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SCARD TEST
(integer) 0
127.0.0.1:6379> SCARD test
(integer) 5
127.0.0.1:6379>
10、判断是不是集合的成员:SISMEMBER key member
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SISMEMBER test abc
(integer) 1
127.0.0.1:6379> SISMEMBER test xoapdpmg
(integer) 0
11、返回n个集合的交集: SINTER key [key ...]
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "444"
127.0.0.1:6379> SADD newtest 123
(integer) 1
127.0.0.1:6379> SMEMBERS newtest
1) "123"
2) "444"
127.0.0.1:6379> SINTER test newtest
1) "123"
127.0.0.1:6379>
12、返回n个集合的交集并且存储:SINTERSTORE dest key [key ...]
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "123"
2) "444"
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
127.0.0.1:6379> SINTERSTORE savetest test newtest
(integer) 1
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
3) "savetest"
127.0.0.1:6379> SMEMBERS savetest
1) "123"
127.0.0.1:6379>
13、返回key集合的差集:SDIFF key [key ...]
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "123"
2) "444"
127.0.0.1:6379> SDIFF test newtest
1) "555"
2) "xiaoming"
3) "apple"
4) "abc"
14、返回key集合的差集并且存储到dest: SDIFFSTORE dest key [key ...]
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> keys *
1) "newtest"
2) "test"
127.0.0.1:6379> SMEMBERS test
1) "abc"
2) "apple"
3) "123"
4) "555"
5) "xiaoming"
127.0.0.1:6379> SMEMBERS newtest
1) "123"
2) "444"
127.0.0.1:6379> SDIFF test newtest
1) "555"
2) "xiaoming"
3) "apple"
4) "abc"
127.0.0.1:6379> KEYS *
1) "newtest"
2) "test"
127.0.0.1:6379> SDIFFSTORE savetest test newtest
(integer) 4
127.0.0.1:6379> EXISTS savetest
(integer) 1
127.0.0.1:6379> SMEMBERS savetest
1) "555"
2) "xiaoming"
3) "apple"
4) "abc"