Redis命令-Set

sadd [key] [member1] [member2] … [membern]

向指定key中添加一个或多个元素,如果key不存在,则会先创建key,已经存在于集合中的元素将会被忽略

如果key存在且不是集合类型,则会返回一个错误

Redis 2.4 版本以前,只能添加一个元素

1
2
3
4
5
6
7
8
9
10
11
# 正常情况
127.0.0.1:6379> sadd sadd-key a b c d e
(integer) 5

# key不是集合类型
127.0.0.1:6379> set str-key 1
OK
127.0.0.1:6379> type str-key
string
127.0.0.1:6379> sadd str-key a
(error) WRONGTYPE Operation against a key holding the wrong kind of value

scard [key]

获取集合的成员数,key不存在时返回0

1
2
3
4
5
6
7
8
9
# 正常情况
127.0.0.1:6379> scard sadd-key
(integer) 5

# key不存在
127.0.0.1:6379> exists scard-key
(integer) 0
127.0.0.1:6379> scard sscard-key
(integer) 0

sdiff [key1] [key2] … [keyn]

获取指定集合之间的差集,不存在的集合视为空集,取值标准是key1,返回结果来自key1,不是后面的集合,也不是所有集合的差集,返回值是差集成员列表

如果key1为空集或者不存在,返回的结果也是空集

1
2
3
4
5
6
7
8
9
10
127.0.0.1:6379> sadd sdiff-key a b c d e
(integer) 5
127.0.0.1:6379> sadd sdiff-key2 c d e g
(integer) 4
127.0.0.1:6379> sadd sdiff-key3 e g a
(integer) 3
127.0.0.1:6379> sadd sdiff-key4 a c
(integer) 2
127.0.0.1:6379> sdiff sdiff-key sdiff-key2 sdiff-key3 sdiff-key4
1) "b"

sdiffstore [destination-key] [key1] [key2] … [keyn]

作用类似sdiff,区别在于会将差集结果存储到destination-key,并且返回差集的元素数量

1
2
3
4
5
6
127.0.0.1:6379> exists sdiff-key5
(integer) 0
127.0.0.1:6379> sdiffstore sdiff-key5 sdiff-key sdiff-key2 sdiff-key3 sdiff-key4
(integer) 1
127.0.0.1:6379> smembers sdiff-key5
1) "b"

sinter [key1] [key2] … [keyn]

获取指定所有集合的交集,不存在的key视为空集,当给定集合中有一个空集时,返回的结果也是空集

1
2
3
4
5
6
7
8
9
127.0.0.1:6379> sadd sinter-key a b c d e
(integer) 5
127.0.0.1:6379> sadd sinter-key2 a b e g
(integer) 4
127.0.0.1:6379> sadd sinter-key3 a b d f h
(integer) 5
127.0.0.1:6379> sinter sinter-key sinter-key2 sinter-key3
1) "b"
2) "a"

sinterstore [destination-key] [key1] [key2] … [keyn]

作用与sinter类似,区别在于会将结果存储到destination-key,并且返回交集的元素数量

1
2
3
4
5
127.0.0.1:6379> sinterstore sinter-key5  sinter-key sinter-key2 sinter-key3
(integer) 2
127.0.0.1:6379> smembers sinter-key5
1) "b"
2) "a"

sismember [key] [member]

判断指定集合中是否包含成员member,存在返回,否则返回0

key不存在时视为空集

1
2
3
4
5
6
7
8
9
10
11
12
13
# 正常情况
127.0.0.1:6379> sadd sismember-key1 a b c d
(integer) 4
127.0.0.1:6379> sismember sismember-key1 a
(integer) 1
127.0.0.1:6379> sismember sismember-key1 e
(integer) 0

# key不存在
127.0.0.1:6379> exists sismember-key a
(integer) 0
127.0.0.1:6379> sismember sismember-key a
(integer) 0

smembers [key]

获取集合中的全部成员,不存在的集合视为空集

1
2
3
4
5
6
7
8
127.0.0.1:6379> sadd smembers-key a b c d e
(integer) 5
127.0.0.1:6379> smembers smembers-key
1) "d"
2) "b"
3) "c"
4) "a"
5) "e"

somve [source-key] [destination-key] [member]

将source-key的成员member移到destination-key中,成功返回1,失败返回0

如果source-key中没有该member,则返回0

如果destination-key已经包含该member,则仅仅只是移除source-key中的元素

如果source-key或者destination-key不是集合时返回一个错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 正常情况
127.0.0.1:6379> sadd smove-key a b c d e
(integer) 5
127.0.0.1:6379> smove smove-key smove-key2 a
(integer) 1
127.0.0.1:6379> smembers smove-key
1) "c"
2) "b"
3) "e"
4) "d"
127.0.0.1:6379> smembers smove-key2
1) "a"

# source-key不存在member
127.0.0.1:6379> smove smove-key smove-key3 g
(integer) 0
127.0.0.1:6379> smembers smove-key3
(empty list or set)

# source-key或者destination-key不是集合
127.0.0.1:6379> smove str-key smove-key4 a
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> smove smove-key str-key b
(error) WRONGTYPE Operation against a key holding the wrong kind of value

spop [key]

移除并返回集合中一个随机成员,返回被移除的成员,如果集合不存在或者为空返回nil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 正常情况
27.0.0.1:6379> sadd spop-key a b c d e
(integer) 5
127.0.0.1:6379> spop spop-key
"e"
127.0.0.1:6379> spop spop-key
"a"
127.0.0.1:6379> smembers spop-key
1) "b"
2) "c"
3) "d"

# 集合不存在
127.0.0.1:6379> exists spop-key2
(integer) 0
127.0.0.1:6379> spop spop-key2
(nil)

# 集合为空
127.0.0.1:6379> sadd spop-key3 a
(integer) 1
127.0.0.1:6379> spop spop-key3
"a"
127.0.0.1:6379> smembers spop-key3
(empty list or set)
127.0.0.1:6379> spop spop-key3
(nil)

srandmember [key] [count]

返回集合中的随机成员,如果没有提供count参数,则返回一个成员,集合为空时返回nill;如果提供了count参数,那么返回一个数组,集合为空时返回空数组.

Redis 2.6版本开始时增加了count参数

count>0:如果count小于集合基数,则返回包含count个成员的数组,数组元素各不相同,如果count大于等于基数,则返回整个集合

count<0:返回一个数组,数组中元素可能会重复多次,长度为count绝对值

该命令和spop的区别在于只返回元素,不会对集合进行任何操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
127.0.0.1:6379> sadd srandmember-key a b c d e
(integer) 5
127.0.0.1:6379> smembers srandmember-key
1) "d"
2) "b"
3) "c"
4) "a"
5) "e"

# 没有提供count参数,且集合不为空
127.0.0.1:6379> srandmember srandmember-key
"e"
127.0.0.1:6379> smembers srandmember-key
1) "c"
2) "a"
3) "b"
4) "e"
5) "d"

# 没有提供count参数,且集合为空
127.0.0.1:6379> exists srandmember-key2
(integer) 0
127.0.0.1:6379> smembers srandmember-key2
(empty list or set)
127.0.0.1:6379> srandmember srandmember-key2
(nil)

# 提供count参数,且count>0,并小于集合成员数量
127.0.0.1:6379> srandmember srandmember-key 3
1) "b"
2) "c"
3) "a"

# 提供count参数,且count>0,并大于集合成员数量
127.0.0.1:6379> srandmember srandmember-key 8
1) "b"
2) "a"
3) "c"
4) "e"
5) "d"

# 提供count参数,且count>0,并等于集合成员数量
127.0.0.1:6379> srandmember srandmember-key 5
1) "b"
2) "a"
3) "c"
4) "e"
5) "d"

# 提供count参数,且count<0
127.0.0.1:6379> srandmember srandmember-key -8
1) "c"
2) "a"
3) "b"
4) "a"
5) "c"
6) "a"
7) "a"
8) "a"

# 提供count参数,且集合为空
127.0.0.1:6379> exists srandmember-key2
(integer) 0
127.0.0.1:6379> smembers srandmember-key2
(empty list or set)
127.0.0.1:6379> srandmember srandmember-key2 5
(empty list or set)

srem [key] [member1] [member2] … [membern]

移除集合中的一个或多个成员,返回成功移除的数量,不包含被忽略的成员

Redis 2.4 版本以前,srem只接受一个参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> sadd srem-key a b c d e f 
(integer) 6
127.0.0.1:6379> smembers srem-key
1) "c"
2) "a"
3) "b"
4) "f"
5) "e"
6) "d"
127.0.0.1:6379> srem srem-key a b f g h
(integer) 3
127.0.0.1:6379> smembers srem-key
1) "c"
2) "e"
3) "d"

sunion [key1] [key2] [key3]

返回所有集合的并集,不存在的集合被视为空集

此处结果有问题,返回的集合数据不是全部的并集,并且结果数量根据key位置的不同发生变化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
127.0.0.1:6379> sadd sunion-key a b
(integer) 2
127.0.0.1:6379> sadd sunion-key2 b c d
(integer) 3
127.0.0.1:6379> sadd sunion-key3 c e f
(integer) 3
127.0.0.1:6379> sunion suinon-key sunion-key2 sunion-key3
1) "f"
2) "c"
3) "b"
4) "e"
5) "d"
127.0.0.1:6379> sunion suinon-key3 sunion-key2 sunion-key
1) "b"
2) "d"
3) "a"
4) "c"

sunionstore [destination-key] [key1] [key2] [key3]

作用与sunion类似,区别在于会将结果储存到destination-key中,返回的是数量

1
2
3
4
5
6
7
8
127.0.0.1:6379> sunionstore sunion-key4 suinon-key sunion-key2 sunion-key3
(integer) 5
127.0.0.1:6379> smembers sunion-key4
1) "f"
2) "c"
3) "b"
4) "e"
5) "d"

sscan [key] [cursor] match [pattern] count [count]

迭代集合中键的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
127.0.0.1:6379> sadd sscan-key hello hi haha redis world
(integer) 5
127.0.0.1:6379> smembers sscan-key
1) "hi"
2) "hello"
3) "haha"
4) "redis"
5) "world"
127.0.0.1:6379> sscan sscan-key 0
1) "0"
2) 1) "hi"
2) "hello"
3) "redis"
4) "haha"
5) "world"
127.0.0.1:6379> sscan sscan-key 0 match h*
1) "0"
2) 1) "hi"
2) "hello"
3) "haha"
127.0.0.1:6379> sscan sscan-key 0 match h* count 2
1) "2"
2) 1) "hi"
2) "hello"

本文作者:Jormin
本文地址https://blog.lerzen.com/2018/01/16/redis命令-set/
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!

----- 到这结束咯 感谢您的阅读 -----