当前位置:网站首页>[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:00 【Bulst】
Fruit seckill business
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

Fruit entities

/** * 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 !");
}
边栏推荐
- 2138. 将字符串拆分为若干长度为 k 的组
- 怎么把mdf和ldf文件导入MySQL workbench中
- From the Huawei weautomate digital robot forum, we can see the "new wisdom of government affairs" in the field of government and enterprises
- 2022-06-23: given a nonnegative array, select any number to make the maximum cumulative sum a multiple of 7, and return the maximum cumulative sum. N is larger, to the 5th power of 10. From meituan. 3
- 216. combined summation III enumeration method
- 数组相向指针系列
- 基于单片机开发的酒精浓度测试仪方案
- Double pointer analog
- 阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
- 1844. replace all numbers with characters
猜你喜欢

China chip Unicorn Corporation

MySQL data (Linux Environment) scheduled backup

Spark - the number of leftouterjoin results is inconsistent with that of the left table

Data midrange: detailed explanation of the technical stack of data acquisition and extraction

Spark - LeftOuterJoin 结果条数与左表条数不一致
![[use picgo+ Tencent cloud object to store cos as a map bed]](/img/14/d650960cc77385504ea5e2e138bd46.jpg)
[use picgo+ Tencent cloud object to store cos as a map bed]

Data middle office: middle office architecture and overview

What is graph neural network? Figure what is the use of neural networks?

目标检测系列——Fast R-CNN

Huawei Router: IPSec Technology
随机推荐
什么是图神经网络?图神经网络有什么用?
MySQL | 视图《康师傅MySQL从入门到高级》笔记
One article explains in detail | those things about growth
Essay - Reflection
Spark - LeftOuterJoin 结果条数与左表条数不一致
Data middle office: middle office practice and summary
MySQL data (Linux Environment) scheduled backup
Pytorch读入据集(典型数据集及自定义数据集两种模式)
How to import MDF and LDF files into MySQL workbench
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
陆奇:我现在最看好这四大技术趋势
嵌入式 | 硬件转软件的几条建议
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
解决:模型训练时loss出现nan
【PyTorch基础教程30】DSSM双塔模型代码解析
十二、所有功能实现效果演示
"Unusual proxy initial value setting is not supported", causes and Solutions
Squid代理服务器应用
Scheme of alcohol concentration tester based on single chip microcomputer
mysql写的代码数据 增删查改等等