Jingdong Jingdou lottery solution
“This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”
I. Business scenario analysis of Jingdong Jingdou Lottery
- Click Jingdong on the home page of jingdong app to enter the Smoking Jingdou page
- Each award is different
The probability of
Opportunity. Beijing bean has a 1/4 chance - can
repeat
Sweepstakes, prize pool probabilities remain the same
Two, jingdong Jingdou lottery technical scheme
Beijing beans lottery is generally used redis set set to operate, then why is the set set applicable to the lottery?
- The set is characterized by
Elements do not repeat
(1 jingdou 5 Jingdou 10 Jingdou thank you for participating) - Set set support
Random reads
The specific technical scheme is to use the set set srandmember command to achieve a random return set element
Three, case combat: SpringBoot+Redis to achieve jingdong Jingdou lottery
Step 1: Initialize the prize
Since the set set is not duplicated, a sequence number is set for each prize when the prize is initialized.
/** * Flush the data to the Redis cache first. * /
@PostConstruct
public void init(a){
boolean bo=this.redisTemplate.hasKey(Constants.PRIZE_KEY);
if(! bo){// Initialize the prize with a probability
List<String> crowds=this.prize();
crowds.forEach(t->this.redisTemplate.opsForSet().add(Constants.PRIZE_KEY,t)); }}/** * Initializes the prize with a probability */
public List<String> prize(a) {
List<String> list=new ArrayList<>();
//10 Peking beans, probability 10%
for (int i = 0; i < 10; i++) {
list.add("10 -"+i);
}
//5 Peking beans, 20% chance
for (int i = 0; i < 20; i++) {
list.add("5 -"+i);
}
//1 Peking bean, probability 60%
for (int i = 0; i < 60; i++) {
list.add("1 -"+i);
}
//0 Beijing beans, probability 10%
for (int i = 0; i < 10; i++) {
list.add("0"+i);
}
return list;
}
Copy the code
Step 2: Raffle
@GetMapping(value = "/prize")
public String prize(a) {
String result="";
try {
// Select 1 time at random.
String object = (String)this.redisTemplate.opsForSet().randomMember(Constants.PRIZE_KEY);
if(! StringUtils.isEmpty(object)){// Intercept the serial number such as 10-1
int temp=object.indexOf(The '-');
int no=Integer.valueOf(object.substring(0,temp));
switch (no){
case 0:
result="What a pity!";
break;
case 1:
result="Get a King bean";
break;
case 5:
result="Obtain 5 Peking Beans.";
break;
case 10:
result="Earn 10 Peking Beans.";
break;
default:
result="What a pity!";
}
}
log.info("Query result: {}", object);
} catch (Exception ex) {
log.error("exception:", ex);
}
return result;
}
Copy the code
Redis distributed cache family
- Redis Distributed Cache (1) – Redis Installation (Linux and Docker)
- Redis Distributed cache (ii) – RDB and AOF
- SpringBoot integrates Mybatis-Plus,Redis and Swagger
- Redis distributed cache (4) – SpringCache integrates redis
- Redis Distributed Cache (5) — Common command (String)
- Redis distributed cache (vi) – article read volume PV solution
- Redis Distributed cache (7) – distributed global ID solution
- Redis Distributed Cache (8) — Highly Concurrent atomic Operations (Redis+Lua)
- Redis Distributed cache (NINE) – a hacker anti-brush attack solution
- Redis Distributed Cache (10) common Commands (Hash)
- Redis Distributed Cache (11) stores Java Bean object business profile one by one
- Redis distributed Cache (twelve) — short link solution
- Redis Distributed Cache (13) — JD Shopping cart solution
- Redis distributed cache (14) – distributed session inconsistency solution
- Redis Distributed Cache (15) – a highly available user registration solution
- Redis distributed Cache (16) – user microblogging solution
- Redis Distributed cache (17)
- Redis Distributed Cache (18) – one – blacklist verifier solution
- The article continues to be updated…