定义
存储结构
整数数组:元素都为整数且节点数量小于等于512(set-max-intset-entries)
字典:元素当中有一个不是整数或者节点数量大于512
命令
- SADD
- SISMEMBER
- SPOP
- SRANDMEMBER
- SREM
- SMOVE
- SCARD
- SMEMBERS
- SSCAN
- SINTER
- SINTERSTORE
- SUNION
- SUNIONSTORE
- SDIFF
- SDIFFSTORE
应用
不允许重复
1. 用户抽奖
# 添加抽奖用户
127.0.0.1:6379> sadd Award 10001 10002 10003 10004 10005 10006
(integer) 6
127.0.0.1:6379> SADD Award 10009
(integer) 1
# 查看所有抽奖用户
127.0.0.1:6379> SMEMBERS Award
1) "10001"
2) "10002"
3) "10003"
4) "10004"
5) "10005"
6) "10006"
7) "10009"
# 抽取1名幸运用户
127.0.0.1:6379> SRANDMEMBER Award
"10001"
127.0.0.1:6379> SRANDMEMBER Award
"10001"
127.0.0.1:6379> SRANDMEMBER Award
"10002"
# 抽取多名幸运用户
127.0.0.1:6379> SRANDMEMBER Award 3
1) "10009"
2) "10002"
3) "10003"
127.0.0.1:6379> SRANDMEMBER Award 3
1) "10001"
2) "10002"
3) "10003"
# 抽取一等奖1名,二等奖3名,三等奖5名,该如何操作? TODO
求交集
2. 共同关注的人或物
# 添加关注的人
127.0.0.1:6379> SADD Follow:A king nanxi qiao tao xingyi
(integer) 5
127.0.0.1:6379> SADD Follow:B king tao xiye
(integer) 3
# 列出共同关注的人
127.0.0.1:6379> SINTER Follow:A Follow:B
1) "tao"
2) "king"