无序集合命令——Redis学习笔记

1:SADD key member [member …]
添加一个或多个成员到无序集合

1
2
3
4
5
6
7
127.0.0.1:6379> sadd myset 'hello'
(integer) 1
127.0.0.1:6379> sadd myset 'world'
(integer) 1
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"

2:SCARD key
获取无序集合成员的数量

1
2
3
4
5
127.0.0.1:6379> smembers myset
1) "world"
2) "hello"
127.0.0.1:6379> scard myset
(integer) 2

3:SDIFF key [key …]
返回第一组集合和其他集合的差集

1
2
3
4
5
6
7
127.0.0.1:6379> sadd myset1 a b c
(integer) 3
127.0.0.1:6379> sadd myset2 c d e
(integer) 3
127.0.0.1:6379> sdiff myset1 myset2
1) "a"
2) "b"

4:SDIFFSTORE destination key [key …]
返回并存储第一组集合和其他集合的差集

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> sadd myset1 a b c
(integer) 3
127.0.0.1:6379> sadd myset2 c d e
(integer) 3
127.0.0.1:6379> sdiff myset1 myset2
1) "a"
2) "b"
127.0.0.1:6379> sdiffstore myset3 myset1 myset2
(integer) 2
127.0.0.1:6379> smembers myset3
1) "a"
2) "b"

5:SINTER key [key …]
返回集合的交集

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> smembers myset1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> smembers myset2
1) "d"
2) "e"
3) "c"
127.0.0.1:6379> sinter myset1 myset2
1) "c"

6:SINTERSTORE destination key [key …]
返回并存储集合的交集

1
2
3
4
5
6
127.0.0.1:6379> sinter myset1 myset2
1) "c"
127.0.0.1:6379> sinterstore myset4 myset1 myset2
(integer) 1
127.0.0.1:6379> smembers myset4
1) "c"

7:SISMEMBER key member
返回集合中是否存在某个键

1
2
3
4
127.0.0.1:6379> smembers myset4
1) "c"
127.0.0.1:6379> sismember myset4 c
(integer) 1

8:SMEMBERS key
返回集合中所有的成员

1
2
3
4
127.0.0.1:6379> smembers myset1
1) "c"
2) "b"
3) "a"

9:SMOVE source destination member
移动集合某个成员到另一个集合中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1:6379> smembers myset1
1) "c"
2) "b"
3) "a"
127.0.0.1:6379> smembers myset2
1) "d"
2) "e"
3) "c"
127.0.0.1:6379> smove myset1 myset2 a
(integer) 1
127.0.0.1:6379> smembers myset1
1) "c"
2) "b"
127.0.0.1:6379> smembers myset2
1) "d"
2) "e"
3) "c"
4) "a"

10:SPOP key [count]
随机移除某个集合成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
27.0.0.1:6379> smembers myset5
1) "c"
2) "b"
3) "a"
4) "d"
5) "e"
6) "f"
127.0.0.1:6379> spop myset5
"d"
127.0.0.1:6379> smembers myset5
1) "c"
2) "e"
3) "a"
4) "b"
5) "f"

11:SRANDMEMBER key [count]
随机取出一个或多个集合成员

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
127.0.0.1:6379> smembers myset5
1) "c"
2) "e"
3) "a"
4) "b"
5) "f"
127.0.0.1:6379> srandmember myset5
"f"
127.0.0.1:6379> srandmember myset5
"f"
127.0.0.1:6379> srandmember myset5
"b"
127.0.0.1:6379> srandmember myset5 3
1) "c"
2) "a"
3) "b"

12:SREM key member [member …]
删除集合一个或多个成员

1
2
3
4
5
6
7
8
9
10
11
12
127.0.0.1:6379> smembers myset5
1) "c"
2) "e"
3) "a"
4) "b"
5) "f"
127.0.0.1:6379> srem myset5 c
(integer) 1
127.0.0.1:6379> srem myset5 c
(integer) 0
127.0.0.1:6379> srem myset5 e a
(integer) 2

13:SUNION key [key …]
获取多个集合的并集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
127.0.0.1:6379> sadd myset1 a b c
(integer) 3
127.0.0.1:6379> sadd myset2 c d e
(integer) 3
127.0.0.1:6379> sadd myset3 a f g
(integer) 3
127.0.0.1:6379> sunion myset1 myset2 myset3
1) "d"
2) "c"
3) "e"
4) "g"
5) "b"
6) "a"
7) "f"

14:SUNIONSTORE destination key [key …]
获取并存储多个集合的并集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1:6379> sunion myset1 myset2 myset3
1) "d"
2) "c"
3) "e"
4) "g"
5) "b"
6) "a"
7) "f"
127.0.0.1:6379> sunionstore myset4 myset1 myset2 myset3
(integer) 7
127.0.0.1:6379> smembers myset4
1) "d"
2) "c"
3) "e"
4) "g"
5) "b"
6) "a"
7) "f"