Dear friends, 2020 is about to end in the last two weeks, and many companies have begun preparations for the annual meeting one after another. At the annual meeting, the most anticipated program is undoubtedly the lucky draw.
One day, the leader told you "Little Fatty, the company will have an annual meeting soon. Please write us a lottery program." Then, you opened Eclipse silently and started writing Math.random().
Today, Brother Feng tells you that for the lottery, you don't need to be so troublesome, you can consider using the Redis set collection.
We all know that the two major characteristics of set are de-duplication and disorder. Similarly, Redis set also meets these two characteristics.
Let's first take a look at what usages Redis set provides to us (you can find that most of the set-related commands start with s).
127.0.0.1:6379> help @set
SADD key member [member ...]
summary: Add one or more members to a set
since: 1.0.0
SCARD key
summary: Get the number of members in a set
since: 1.0.0
SDIFF key [key ...]
summary: Subtract multiple sets
since: 1.0.0
SDIFFSTORE destination key [key ...]
summary: Subtract multiple sets and store the resulting set in a key
since: 1.0.0
SINTER key [key ...]
summary: Intersect multiple sets
since: 1.0.0
SINTERSTORE destination key [key ...]
summary: Intersect multiple sets and store the resulting set in a key
since: 1.0.0
SISMEMBER key member
summary: Determine if a given value is a member of a set
since: 1.0.0
SMEMBERS key
summary: Get all the members in a set
since: 1.0.0
SMOVE source destination member
summary: Move a member from one set to another
since: 1.0.0
SPOP key [count]
summary: Remove and return one or multiple random members from a set
since: 1.0.0
SRANDMEMBER key [count]
summary: Get one or multiple random members from a set
since: 1.0.0
SREM key member [member ...]
summary: Remove one or more members from a set
since: 1.0.0
SSCAN key cursor [MATCH pattern] [COUNT count]
summary: Incrementally iterate Set elements
since: 2.8.0
SUNION key [key ...]
summary: Add multiple sets
since: 1.0.0
SUNIONSTORE destination key [key ...]
summary: Add multiple sets and store the resulting set in a key
since: 1.0.0
Let's take a look at a few commonly used ones.
127.0.0.1:6379> SADD k1 a a b b c d //在名为k1的集合中添加abcd四个元素
(integer) 4
127.0.0.1:6379> SADD k2 b c d e
(integer) 4
127.0.0.1:6379> SCARD k1 //列出集合的大小
(integer) 4
127.0.0.1:6379> SMEMBERS k1 //列出集合中所有元素,可以看出“去重且乱序”的
1) "c"
2) "a"
3) "d"
4) "b"
127.0.0.1:6379> SISMEMBER k1 a //集合k1是否包含a
(integer) 1
127.0.0.1:6379> SISMEMBER k1 aa
(integer) 0
127.0.0.1:6379> sdiff k1 k2 //集合k1和k2的差异,k1,k2是有顺序的(k1-k2)
1) "a"
127.0.0.1:6379> sdiff k2 k1
1) "e"
127.0.0.1:6379> SUNION k1 k2 // 并集
1) "a"
2) "d"
3) "b"
4) "c"
5) "e"
127.0.0.1:6379> SINTER k1 k2 //交集
1) "c"
2) "d"
3) "b"
127.0.0.1:6379> SRANDMEMBER k1 //从集合中随机取出(get)一个元素
"c"
127.0.0.1:6379> SRANDMEMBER k1
"b"
127.0.0.1:6379> SRANDMEMBER k1 3 //从集合中随机(这不废话吗,反正set也是无序的)取出3个元素
1) "c"
2) "d"
3) "b"
127.0.0.1:6379> SRANDMEMBER k1 3
1) "a"
2) "d"
3) "b"
127.0.0.1:6379> SPOP k1 3 //从集合中随机取出(remove)3个元素
1) "d"
2) "a"
3) "b"
127.0.0.1:6379> SPOP k1 2
1) "c"
You may have noticed this. The lottery can be done with SRANDMEMBER or SPOP.
1. Number of prizes> number of draws
Assuming that a company of 3 people has prepared 5 prizes, it can be repeated.
127.0.0.1:6379> sadd lotteryList tom jerry tony
(integer) 3
127.0.0.1:6379> SRANDMEMBER lotteryList -5 //如果为负数,会首先满足返回元素个数,可能会有重复
1) "tom"
2) "jerry"
3) "tony"
4) "jerry"
5) "jerry"
2. Number of prizes <number of draws
127.0.0.1:6379> sadd lottoryList tom jerry jack tony lucy lily poly kevin
(integer) 8
127.0.0.1:6379> spop lottoryList 3 //三等奖
1) "jerry"
2) "jack"
3) "lily"
127.0.0.1:6379> spop lottoryList 2 //二等奖
1) "kevin"
2) "poly"
127.0.0.1:6379> spop lottoryList 1 //一等奖
1) "tony"
to sum up
Let's summarize a little bit. For simple random scenes like lottery draws, you can consider another idea based on the actual situation, which is to use Redis's SRANDMEMBER or spop to achieve.
Well, this is the case for this sharing, we will talk about sorted_set next time, I hope you can gain.