添加元素:SADD key member [member …]
获取集合中的所有元素:SMEMBERS key
获取集合中元素个数:SCARD key
127.0.0.1:6379> sadd s1 a b c d e a b
(integer) 5
127.0.0.1:6379> scard s1
(integer) 5
127.0.0.1:6379> smembers s1
1) "c"
2) "b"
3) "a"
4) "e"
5) "d"
set是无序的,不重复的。
SREM key member [member …]
127.0.0.1:6379> smembers s1
1) "c"
2) "b"
3) "a"
4) "e"
5) "d"
127.0.0.1:6379> srem s1 a
(integer) 1
127.0.0.1:6379> smembers s1
1) "b"
2) "c"
3) "e"
4) "d"
127.0.0.1:6379> srem s1 b c
(integer) 2
127.0.0.1:6379> smembers s1
1) "e"
2) "d"
SISMEMBER key member
存在返回1,不存在返回0
127.0.0.1:6379> smembers s1
1) "e"
2) "d"
127.0.0.1:6379> sismember s1 a
(integer) 0
127.0.0.1:6379> sismember s1 e
(integer) 1
127.0.0.1:6379> sismember s1 d
(integer) 1
127.0.0.1:6379> sismember s1 c
(integer) 0
SRANDMEMBER key [数字]
127.0.0.1:6379> sadd s1 a b c
(integer) 3
127.0.0.1:6379> smembers s1
1) "a"
2) "c"
3) "b"
4) "e"
5) "d"
127.0.0.1:6379> srandmember s1 1
1) "b"
127.0.0.1:6379> srandmember s1 2
1) "b"
2) "d"
127.0.0.1:6379> srandmember s1 3
1) "a"
2) "e"
3) "d"
127.0.0.1:6379> srandmember s1 3
1) "a"
2) "e"
3) "d"
127.0.0.1:6379> srandmember s1 4
1) "a"
2) "c"
3) "b"
4) "d"
127.0.0.1:6379> smembers s1
1) "a"
2) "c"
3) "b"
4) "e"
5) "d"
SPOP key
127.0.0.1:6379> smembers s1
1) "a"
2) "c"
3) "b"
4) "e"
5) "d"
127.0.0.1:6379> spop s1
"a"
127.0.0.1:6379> smembers s1
1) "c"
2) "b"
3) "e"
4) "d"
127.0.0.1:6379> spop s1
"d"
127.0.0.1:6379> smembers s1
1) "c"
2) "b"
3) "e"
SMOVE sourceKey destKey member
127.0.0.1:6379> smembers s1
1) "c"
2) "b"
3) "e"
127.0.0.1:6379> sadd s1 a f
(integer) 2
127.0.0.1:6379> smembers s1
1) "a"
2) "f"
3) "c"
4) "b"
5) "e"
127.0.0.1:6379> smove s1 s2 a
(integer) 1
127.0.0.1:6379> smembers s1
1) "f"
2) "c"
3) "b"
4) "e"
127.0.0.1:6379> smembers s2
1) "a"
127.0.0.1:6379> smove s1 s2 e
(integer) 1
127.0.0.1:6379> smembers s1
1) "f"
2) "c"
3) "b"
127.0.0.1:6379> smembers s2
1) "a"
2) "e"
SDIFF key [key …]
127.0.0.1:6379> del s1
(integer) 1
127.0.0.1:6379> del s2
(integer) 1
127.0.0.1:6379> sadd s1 a b c d
(integer) 4
127.0.0.1:6379> sadd s2 c d e f
(integer) 4
127.0.0.1:6379> sdiff s1 s2
1) "a"
2) "b"
127.0.0.1:6379> sdiff s2 s1
1) "f"
2) "e"
127.0.0.1:6379> sdiffstore s3 s1 s2
(integer) 2
127.0.0.1:6379> smembers s3
1) "a"
2) "b"
127.0.0.1:6379> smembers s1
1) "a"
2) "d"
3) "c"
4) "b"
127.0.0.1:6379> smembers s2
1) "f"
2) "d"
3) "e"
4) "c"
127.0.0.1:6379> sdiffstore s4 s2 s1
(integer) 2
127.0.0.1:6379> smembers s4
1) "f"
2) "e"
SINTER key [key …]
127.0.0.1:6379> del s1
(integer) 1
127.0.0.1:6379> del s2
(integer) 1
127.0.0.1:6379> sadd s1 a b c d
(integer) 4
127.0.0.1:6379> sadd s2 c d e f
(integer) 4
127.0.0.1:6379> sinter s1 s2
1) "d"
2) "c"
127.0.0.1:6379> sinter s2 s1
1) "d"
2) "c"
127.0.0.1:6379> sinterstore s3 s1 s2
(integer) 2
127.0.0.1:6379> smembers s3
1) "d"
2) "c"
127.0.0.1:6379> smembers s1
1) "a"
2) "d"
3) "c"
4) "b"
127.0.0.1:6379> smembers s2
1) "f"
2) "d"
3) "e"
4) "c"
SUNION key [key …]
127.0.0.1:6379> del s1
(integer) 1
127.0.0.1:6379> del s2
(integer) 1
127.0.0.1:6379> sadd s1 a b c d
(integer) 4
127.0.0.1:6379> sadd s2 c d e f
(integer) 4
127.0.0.1:6379> sunion s1 s2
1) "a"
2) "f"
3) "c"
4) "b"
5) "e"
6) "d"
127.0.0.1:6379> sunion s2 s1
1) "a"
2) "f"
3) "b"
4) "e"
5) "c"
6) "d"
127.0.0.1:6379> sunionstore s3 s1 s2
(integer) 6
127.0.0.1:6379> smembers s3
1) "a"
2) "f"
3) "c"
4) "b"
5) "e"
6) "d"
127.0.0.1:6379> smembers s1
1) "a"
2) "d"
3) "c"
4) "b"
127.0.0.1:6379> smembers s2
1) "f"
2) "d"
3) "e"
4) "c"
添加抽奖用户:sadd users 抽奖用户id
显示参与用户个数:scard users
显示中奖用户:srandmember user 中奖用户个数,这个不会删除参与抽奖的用户,只是每次从里面随机取要的个数;spop users会删除当前随机选择的用户。
127.0.0.1:6379> sadd users 1001 1002 1003 1004 1005
(integer) 5
127.0.0.1:6379> scard users
(integer) 5
127.0.0.1:6379> srandmember users 2
1) "1004"
2) "1005"
127.0.0.1:6379> srandmember users 2
1) "1001"
2) "1004"
127.0.0.1:6379> spop users
"1002"
127.0.0.1:6379> spop users
"1001"
新增点赞:sadd pub:msgID 点赞用户ID1 点赞用户ID2
取消点赞:srem pub:msgID 点赞用户ID
展现所有点赞过的用户:smembers pub:msgID
点赞用户数统计:scard pub:msgID
判断某个朋友是否对当前用户点赞过:sismember pub:msgID 用户ID
127.0.0.1:6379> sadd pub:msgID 1001 1002 1003 1004
(integer) 4
127.0.0.1:6379> srem pub:msgID 1002
(integer) 1
127.0.0.1:6379> smembers pub:msgID
1) "1001"
2) "1003"
3) "1004"
127.0.0.1:6379> scard pub:msgID
(integer) 3
127.0.0.1:6379> sismember pub:msgID 1002
(integer) 0
127.0.0.1:6379> sismember pub:msgID 1001
(integer) 1
127.0.0.1:6379> sadd user1 1001 1002 1003 1004
(integer) 4
127.0.0.1:6379> sadd user2 1002 1004 1005 1006
(integer) 4
127.0.0.1:6379> sinter user1 user2
1) "1002"
2) "1004"
user1001 有QQ好友userId为 1002 1003 1004 1005
user1002 有QQ好友userId 为 1001 1004 1005 1006
user1001和user1002互为好友,那么给user1001推荐肯认识的人,就是user1001-user1002的差集且还不包含user1002
127.0.0.1:6379> sadd user1 1001 1002 1003 1004
(integer) 4
127.0.0.1:6379> sadd user2 1002 1004 1005 1006
(integer) 4
127.0.0.1:6379> sinter user1 user2
1) "1002"
2) "1004"
127.0.0.1:6379> sadd user1001 1002 1003 1004 1005
(integer) 4
127.0.0.1:6379> sadd user1002 1001 1004 1005 1006
(integer) 4
127.0.0.1:6379> sismember user1001 1002
(integer) 1
127.0.0.1:6379> sismember user1002 1001
(integer) 1
127.0.0.1:6379> sdiff user1001 user1002
1) "1002"
2) "1003"
127.0.0.1:6379> sdiffstore cj user1001 user1002
(integer) 2
127.0.0.1:6379> smembers cj
1) "1002"
2) "1003"
127.0.0.1:6379> srem cj 1002
(integer) 1
127.0.0.1:6379> smembers cj
1) "1003"