redis set 是string类型对象的无序集合,set不管存储多少对象,对存储对象的add,remove和test操作的时间复杂度是O(1)。set最多能包含 232 - 1 个member。
语法:sadd key member[member...]
解释:对特定key的set增加一个或多个值,返回是增加元素的个数。注意:对同一个member多次add,set中只会保留一份。
1. [root@xsf001 ~]# redis-cli
2. redis 127.0.0.1:6379> sadd stu zhangsan lisi wangwu #新增
3. (integer) 3
4. redis 127.0.0.1:6379> smembers stu #得到set的所有member
5. 1) "wangwu"
6. 2) "lisi"
7. 3) "zhangsan"
8. redis 127.0.0.1:6379> sadd stu zhangsan #增加存在的member
9. (integer) 0
10.redis 127.0.0.1:6379> smembers stu
11. 1) "wangwu"
12.2) "lisi"
13. 3) "zhangsan"
14.redis 127.0.0.1:6379> sadd tech wangwu liming joe
15. (integer) 3
16.redis 127.0.0.1:6379> sadd tech jim
17. (integer) 1
18.redis 127.0.0.1:6379> smembers tech
19. 1) "jim"
20.2) "liming"
21. 3) "wangwu"
22.4) "joe"
语法:smembers key
解释:获取set中的所有member
1. redis 127.0.0.1:6379> smembers stu
2. 1) "wangwu"
3. 2) "lisi"
4. 3) "zhangsan"
5. redis 127.0.0.1:6379> smembers tech
6. 1) "jim"
7. 2) "liming"
8. 3) "wangwu"
9. 4) "joe"
语法:sismember key member
解释:判断值是否是set的member。如果值是set的member返回1,否则,返回0
1. redis 127.0.0.1:6379> sismember tech jim #jim 是set的member
2. (integer) 1
3. redis 127.0.0.1:6379> sismember tech jim001 #jim001 不是set的member
4. (integer) 0
语法:scard key
解释:返回set的member个数,如果set不存在,返回0
1. redis 127.0.0.1:6379> scard tech # tech 存在
2. (integer) 4
3. redis 127.0.0.1:6379> scard stud #stud 不存在
4. (integer) 0
5. redis 127.0.0.1:6379> scard stu
6. (integer) 4
语法:srandmember key
解释:从set中返回一个随机member
1. redis 127.0.0.1:6379> srandmember stu
2. "zhangsan"
3. redis 127.0.0.1:6379> srandmember stu
4. "zhangsan"
5. redis 127.0.0.1:6379> srandmember stu
6. "wangwu"
7. redis 127.0.0.1:6379> srandmember stu
8. "zhangsan01"
语法:spop key
解释:移除并返回一个随机member
1. redis 127.0.0.1:6379> smembers stu #pop前
2. 1) "zhangsan01"
3. 2) "wangwu"
4. 3) "lisi"
5. 4) "zhangsan"
6. redis 127.0.0.1:6379> spop stu #移除一个随机member
7. "lisi"
8. redis 127.0.0.1:6379> smembers stu #pop后
9. 1) "zhangsan01"<span style="white-space:pre"> </span>
10.2) "wangwu"
11. 3) "zhangsan"
语法:srem key member [member ...]
解释:移除一个或多个member
1. redis 127.0.0.1:6379> smembers tech
2. 1) "jim"
3. 2) "liming"
4. 3) "wangwu"
5. 4) "joe"
6. redis 127.0.0.1:6379> srem tech jim #移除jim
7. (integer) 1
8. redis 127.0.0.1:6379> smembers tech
9. 1) "liming"
10.2) "wangwu"
11. 3) "joe"
12.redis 127.0.0.1:6379> srem tech liming joe #移除多个member
13. (integer) 2
14.redis 127.0.0.1:6379> smembers tech
15. 1) "wangwu"
语法:smove source destination member
解释:将source中的member移动到destination
1. redis 127.0.0.1:6379> smembers tech #smove前
2. 1) "wangwu"
3. redis 127.0.0.1:6379> smembers stu
4. 1) "zhangsan01"
5. 2) "wangwu"
6. 3) "zhangsan"
7. redis 127.0.0.1:6379> smove stu tech zhangsan #将zhangsan 从stu移动到tech
8. (integer) 1
9. redis 127.0.0.1:6379> smembers stu #smove后
10.1) "zhangsan01"
11. 2) "wangwu"
12.redis 127.0.0.1:6379> smembers tech
13. 1) "wangwu"
14.2) "zhangsan"
语法:sunion key[key...]
解释:多个set的并集
1. redis 127.0.0.1:6379> smembers stu
2. 1) "zhangsan01"
3. 2) "wangwu"
4. redis 127.0.0.1:6379> sunion stu
5. 1) "zhangsan01"
6. 2) "wangwu"
7. redis 127.0.0.1:6379> smembers tech
8. 1) "wangwu"
9. 2) "zhangsan"
10.redis 127.0.0.1:6379> sunion stu tech
11. 1) "zhangsan01"
12.2) "wangwu"
13. 3) "zhangsan"
语法:sunionstore destination key [key ...]
解释:求多个set并集,并把结果存储到destination
1. redis 127.0.0.1:6379> sunionstore same stu tech #把stu tech并集结果存储在union
2. (integer) 3
3. redis 127.0.0.1:6379> smembers union
4. 1) "zhangsan01"
5. 2) "wangwu"
6. 3) "zhangsan"
语法:sinter key[key...]
解释:多个set的交集
1. redis 127.0.0.1:6379> smembers stu
2. 1) "zhangsan01"
3. 2) "wangwu"
4. redis 127.0.0.1:6379> smembers tech
5. 1) "wangwu"
6. 2) "zhangsan"
7. redis 127.0.0.1:6379> sinter stu tech
8. 1) "wangwu"
语法:sinterstore destination key [key ...]
解释:把多个set的交集结果存储到destination
1. redis 127.0.0.1:6379> sinterstore inter stu tech
2. (integer) 1
3. redis 127.0.0.1:6379> smembers inter
4. 1) "wangwu"
e) set中在其他set中不存在member
语法:sdiff key[key ...]
1. redis 127.0.0.1:6379>
2. redis 127.0.0.1:6379> smembers stu
3. 1) "zhangsan01"
4. 2) "wangwu"
5. redis 127.0.0.1:6379> smembers tech
6. 1) "wangwu"
7. 2) "zhangsan"
8. redis 127.0.0.1:6379> sdiff stu tech
9. 1) "zhangsan01"
10.redis 127.0.0.1:6379> sdiff tech stu
11. 1) "zhangsan"
语法:sdiffstore key[key...]
1. redis 127.0.0.1:6379> sdiffstore diff stu tech
2. (integer) 1
3. redis 127.0.0.1:6379> smembers diff