---
Redis学习-Set命令使用
---
```
Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的,这就意味着集合中不能出现重复的数据。
Redis 中集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。
```
```
SADD key member [member]
向集合添加一个或多个成员
127.0.0.1:6379> sadd study redis java mysql oracle linux
(integer) 5
SCARD key
获取集合的成员数
127.0.0.1:6379> scard study
(integer) 5
SDIFF key [key]
返回给定所有集合的差集
127.0.0.1:6379> smembers study
1) "mysql"
2) "java"
3) "redis"
4) "oracle"
5) "linux"
127.0.0.1:6379> sadd studys spring hibernate mybatics redis java
(integer) 5
127.0.0.1:6379> sdiff study studys
1) "mysql"
2) "linux"
3) "oracle"
127.0.0.1:6379> sdiff studys study
1) "mybatics"
2) "hibernate"
3) "spring"
SPOP key
移除并返回集合中的一个随机元素
127.0.0.1:6379> spop study
"mysql"
SDIFFSTORE destination key [key]
返回给定所有集合的差集并存储在 destination 中
127.0.0.1:6379> sdiffstore study_1 study studys
(integer) 2
127.0.0.1:6379> scard study_1
(integer) 2
127.0.0.1:6379> smembers study_1
1) "linux"
2) "oracle"
127.0.0.1:6379> smembers study_1
SINTER key [key]
返回给定所有集合的交集
127.0.0.1:6379> sinter study studys
1) "java"
2) "redis"
SINTERSTORE destination key [key]
返回给定所有集合的交集并存储在 destination 中
127.0.0.1:6379> smembers study_2
1) "java"
2) "redis"
SISMEMBER key member
判断 member 元素是否是集合 key 的成员
127.0.0.1:6379> sismember studys linux
(integer) 0
127.0.0.1:6379> sismember studys java
(integer) 1
SMEMBERS key
返回集合中的所有成员
127.0.0.1:6379> smembers study
1) "java"
2) "redis"
3) "oracle"
4) "linux"
SMOVE source destination member
将 member 元素从 source 集合移动到 destination 集合
127.0.0.1:6379> smembers study_2
1) "java"
2) "redis"
127.0.0.1:6379> smembers study_1
1) "linux"
2) "oracle"
127.0.0.1:6379> smove study_1 study_2 java
(integer) 0
127.0.0.1:6379> smove study_1 study_2 linux
(integer) 1
127.0.0.1:6379> smembers study_1
1) "oracle"
127.0.0.1:6379> smembers study_2
1) "java"
2) "redis"
3) "linux"
SRANDMEMBER key [count]
返回集合中一个或多个随机数
127.0.0.1:6379> srandmember study
"redis"
127.0.0.1:6379> srandmember study
"java"
127.0.0.1:6379> srandmember study 1
1) "redis"
127.0.0.1:6379> srandmember study 3
1) "java"
2) "redis"
3) "linux"
SREM key member [member]
移除集合中一个或多个成员
127.0.0.1:6379> sadd addr beijing tianjin shanghai wuhan hangzhou
(integer) 5
127.0.0.1:6379> srem addr beijing tianjin
(integer) 2
127.0.0.1:6379> smembers addr
1) "shanghai"
2) "hangzhou"
3) "wuhan"
127.0.0.1:6379> sismember addr hangzhou
(integer) 1
127.0.0.1:6379> sismember addr zhengzhou
(integer) 0
127.0.0.1:6379> srem addr zhengzhou
(integer) 0
SUNION key [key]
返回所有给定集合的并集
127.0.0.1:6379> sadd addr_1 hangzhou zhengzhou chengdu yuxi
(integer) 4
127.0.0.1:6379> sadd addr_2 shenzhen beijing changsha yuxi chegndu
(integer) 5
127.0.0.1:6379> sunion addr addr_1 addr_2
1) "yuxi"
2) "hangzhou"
3) "wuhan"
4) "zhengzhou"
5) "shanghai"
6) "chengdu"
7) "chegndu"
8) "changsha"
9) "beijing"
10) "shenzhen"
SUNIONSTORE destination key [key]
所有给定集合的并集存储在 destination 集合中
127.0.0.1:6379> sunionstore addr_union addr addr_1 addr_2
(integer) 10
127.0.0.1:6379> smembers addr_union
1) "yuxi"
2) "hangzhou"
3) "wuhan"
4) "zhengzhou"
5) "shanghai"
6) "chengdu"
7) "chegndu"
8) "changsha"
9) "beijing"
10) "shenzhen"
SSCAN key cursor [MATCH pattern] [COUNT count]
迭代集合中的元素
127.0.0.1:6379> sscan addr_union 1
1) "0"
2) 1) "chegndu"
2) "changsha"
3) "shenzhen"
4) "wuhan"
5) "zhengzhou"
6) "yuxi"
7) "chengdu"
127.0.0.1:6379> sscan addr_union 3
1) "0"
2) 1) "yuxi"
2) "chengdu"
127.0.0.1:6379> sscan addr_union 3 count 3
1) "0"
2) 1) "yuxi"
2) "chengdu"
127.0.0.1:6379> sscan addr_union 1 count 3
1) "5"
2) 1) "chegndu"
2) "changsha"
3) "shenzhen"
```