文章目录
- Redis之set类型
- 1. 添加元素/获取集合中的所有元素/获取集合中元素个数
- 2. 删除元素
- 3. 判断元素是否在集合中
- 3. 从集合中随机弹出一个元素,元素不删除
- 4. 从集合中随机弹出元素,出一个删一个
- 5. 将元素从一个集合转移到另外一个集合
- 6. 集合的差集
- 7. 集合的交集
- 8. 集合的并集
- 9. 应用场景
- 9.1 微信抽奖小程序
- 9.2 微信朋友圈点赞
- 9.3 微博好友关注社交关系
- 9.4 QQ内推可能认识的人
Redis之set类型
1. 添加元素/获取集合中的所有元素/获取集合中元素个数
添加元素: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是无序的,不重复的。
2. 删除元素
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"
3. 判断元素是否在集合中
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
3. 从集合中随机弹出一个元素,元素不删除
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"
4. 从集合中随机弹出元素,出一个删一个
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"
5. 将元素从一个集合转移到另外一个集合
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"
6. 集合的差集
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"
7. 集合的交集
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"
8. 集合的并集
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"
9. 应用场景
9.1 微信抽奖小程序
添加抽奖用户: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"
9.2 微信朋友圈点赞
新增点赞: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
9.3 微博好友关注社交关系
- 共同关注的人(集合交集)
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"
9.4 QQ内推可能认识的人
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"