Redis 集合(set) 命令

Redis sunionstore 命令用于返回多个集合的并集并将其存储到指定的key中。不存在的key被认为是空集,如果指定的key已经存在,则被覆盖。

语法:

sunionstore dest_key key1 key2..keyN 

可用版本:

>=1.0.0.

返回值:

整数,结果集中元素的数量。

返回值类型:

整数

示例1:Redis sunionstore

mycolor1 = {R G B}

mycolor2 = {G B Y}

sunionstore dest_key mycolor1 mycolor2 = {R G B Y}

 127.0.0.1:6379> SADD mycolor1 R G B
(integer) 3
127.0.0.1:6379> SADD mycolor2 G B Y
(integer) 3
127.0.0.1:6379> sunionstore  dest_key mycolor1 mycolor2
(integer) 4
127.0.0.1:6379> smembers dest_key
1) "G"
2) "B"
3) "Y"
4) "R" 

示例2:Redis sunionstore 

mycolor1 = {R G B}

mycolor2 = {GB Y}

mycolor3 = {B O P}

sunionstore dest_key mycolor1 mycolor2 mycolor3 = {R G B Y O P}

如果目标键已经存在,则将现有内容清除,并写入这个新集合。

 127.0.0.1:6379> SADD mycolor3 B O P
(integer) 3
127.0.0.1:6379> sunionstore  dest_key mycolor1 mycolor2 mycolor3
(integer) 6
127.0.0.1:6379> smembers dest_key
1) "Y"
2) "R"
3) "O"
4) "P"
5) "G"
6) "B" 

示例3:Redis sunionstore 

mycolor1 = {R G B}

mycolor2 = {G B Y}

mycolor3 = {B O P}

sunionstore mycolor1 mycolor1 mycolor2 mycolor3

127.0.0.1:6379> sunionstore  mycolor1 mycolor1 mycolor2 mycolor3
(integer) 6
127.0.0.1:6379> smembers mycolor1
1) "Y"
2) "R"
3) "O"
4) "P"
5) "G"
6) "B"