current position:Home>[redis implements seckill business ①] seckill process overview | basic business implementation

[redis implements seckill business ①] seckill process overview | basic business implementation

2022-06-24 09:05:19Bulst


friends , Starting today , We use it together Redis To realize seckill business , We need to refuel together !

Our general idea is to design a small fruit spike project .

First we will have a kind of fruit , The fruit has a name 、 Number 、 Start time and end time of the rush purchase .

The business process

  • Whether the second kill starts or ends , If you have not started or finished, you cannot place an order
  • Is there enough stock , If not enough, you can't place an order
     Insert picture description here

Fruit entities

 Insert picture description here

/** *  Fruits  * * @author issavior */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Fruits implements Serializable {
    

    private static final long serialVersionUID = 1;

    private Long id;

    private String name;

    private Integer count;

    private Date startTime;

    private Date endTime;
}

New fruit

Controller

/** * @author issavior */
@RestController
@RequestMapping("/seckill")
public class SeckillController {
    

    @Autowired
    private SeckillService seckillService;


    /** *  New fruit  * @param fruits  Fruits  * @return ResponseEntity<Object> */
    @PostMapping
    public ResponseEntity<Object> insertFruits(@RequestBody Fruits fruits) {
    

        return seckillService.insertFruits(fruits);
    }

}

ServiceImpl

/** * @author issavior */
@Service
public class SeckillServiceImpl implements SeckillService {
    

    @Autowired
    private SeckillMapper seckillMapper;

    @Autowired
    private RedisIdWorker redisIdWorker;

    /** *  New fruit  * * @param fruits  New fruit data  * @return ResponseEntity<Object> */
    @Override
    public ResponseEntity<Object> insertFruits(Fruits fruits) {
    
			
		//  Globally unique ID Tool class , See the previous article in the series for details 
        long id = redisIdWorker.nextId("seckill:fruits");
        fruits.setId(id);
        int successFlag = seckillMapper.insertFruits(fruits);
        if (successFlag == 0) {
    
            return ResponseEntity.status(400).body(" Add failed ");
        }

        return ResponseEntity.ok(" Added successfully ");

    }
}

Seckill business is basically realized

    /** *  Rush to buy fruit  * * @param id  The primary key to rush to buy fruit ID * @return ResponseEntity<Object> */
    @PostMapping("/{id}")
    public ResponseEntity<Object> seckillFruits(@PathVariable("id") Long id) {
    

        return seckillService.seckillFruits(id);
    }

   /** *  Rush to buy fruit  * * @param id  Rush to buy fruit ID * @return ResponseEntity<Object> */
    @Override
    public ResponseEntity<Object> seckillFruits(Long id) {
    

        Fruits fruits = seckillMapper.selectFruits(id);
        if (fruits == null) {
    
            return ResponseEntity.status(400).body(" The fruit is sold out , Come again next time !");
        }

        LocalDateTime startTime = fruits.getStartTime();
        LocalDateTime endTime = fruits.getEndTime();
        Integer count = fruits.getCount();

        if (startTime.isAfter(LocalDateTime.now())) {
    
            return ResponseEntity.status(400).body(" The activity hasn't started yet , Go to the home page of Brest !!");
        }
        if (endTime.isBefore(LocalDateTime.now())) {
    
            return ResponseEntity.status(400).body(" The activity is over , Go to the home page of Brest !!");
        }
        if (count < 1) {
    
            return ResponseEntity.status(400).body(" Out of stock , Go to the home page of Brest !!");
        }

        int updateFruits = seckillMapper.updateFruits(id);

        if (updateFruits == 0) {
    
            return ResponseEntity.status(400).body(" The fruit is sold out , Come again next time !");
        }
        return ResponseEntity.ok(" The rush to buy fruit succeeded , Go to the home page of Brest !");
    }

copyright notice
author[Bulst],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240728174421.html

Random recommended