当前位置:网站首页>【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
2022-06-24 09:05:00 【步爾斯特】
小夥伴們,從今天開始,我們一起用Redis來實現秒殺業務,要一起加油啊!
我們大概的思路就是設計一個水果秒殺小項目。
首先我們會有一種水果,該水果擁有名字、數量、搶購開始時間和搶購結束時間。
業務流程
- 秒殺是否開始或結束,如果尚未開始或已經結束則無法下單
- 庫存是否充足,不足則無法下單
水果實體類
/** * 水果 * * @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;
}
新增水果
Controller
/** * @author issavior */
@RestController
@RequestMapping("/seckill")
public class SeckillController {
@Autowired
private SeckillService seckillService;
/** * 新增水果 * @param 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;
/** * 新增水果 * * @param fruits 新增的水果數據 * @return ResponseEntity<Object> */
@Override
public ResponseEntity<Object> insertFruits(Fruits fruits) {
// 全局唯一ID工具類,詳情可見系列文章上一篇
long id = redisIdWorker.nextId("seckill:fruits");
fruits.setId(id);
int successFlag = seckillMapper.insertFruits(fruits);
if (successFlag == 0) {
return ResponseEntity.status(400).body("新增失敗");
}
return ResponseEntity.ok("新增成功");
}
}
秒殺業務基本實現
/** * 搶購水果 * * @param id 搶購水果的主鍵ID * @return ResponseEntity<Object> */
@PostMapping("/{id}")
public ResponseEntity<Object> seckillFruits(@PathVariable("id") Long id) {
return seckillService.seckillFruits(id);
}
/** * 搶購水果 * * @param id 搶購水果的ID * @return ResponseEntity<Object> */
@Override
public ResponseEntity<Object> seckillFruits(Long id) {
Fruits fruits = seckillMapper.selectFruits(id);
if (fruits == null) {
return ResponseEntity.status(400).body("水果賣光了,下次再來吧!");
}
LocalDateTime startTime = fruits.getStartTime();
LocalDateTime endTime = fruits.getEndTime();
Integer count = fruits.getCount();
if (startTime.isAfter(LocalDateTime.now())) {
return ResponseEntity.status(400).body("活動還沒開始,去步爾斯特的主頁看看吧!!");
}
if (endTime.isBefore(LocalDateTime.now())) {
return ResponseEntity.status(400).body("活動已經結束了,去步爾斯特的主頁看看吧!!");
}
if (count < 1) {
return ResponseEntity.status(400).body("沒有庫存了,去步爾斯特的主頁看看吧!!");
}
int updateFruits = seckillMapper.updateFruits(id);
if (updateFruits == 0) {
return ResponseEntity.status(400).body("水果賣光了,下次再來吧!");
}
return ResponseEntity.ok("搶購水果成功,去步爾斯特的主頁看看吧!");
}
边栏推荐
- "Unusual proxy initial value setting is not supported", causes and Solutions
- [e325: attention] VIM editing error
- 开源之夏中选名单已公示,基础软件领域成为今年的热门申请
- What is SRE? A detailed explanation of SRE operation and maintenance system
- opencv最大值滤波(不局限于图像)
- 1704. judge whether the two halves of a string are similar
- A tip to read on Medium for free
- How to import MDF and LDF files into MySQL workbench
- Floating error waiting for changelog lock
- Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
猜你喜欢
[quantitative investment] discrete Fourier transform to calculate array period
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
[noi Simulation Competition] send (tree DP)
数据中台:数据采集和抽取的技术栈详解
opencv最大值滤波(不局限于图像)
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
陆奇:我现在最看好这四大技术趋势
数据中台:中台架构及概述
110. balanced binary tree recursive method
4275. Dijkstra序列
随机推荐
1844. replace all numbers with characters
Kaformer personal notes
数组相向指针系列
【LeetCode】415. String addition
从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
tcpdump抓包实现过程
Data middle office: a collection of middle office construction architectures of large domestic factories
【量化投资】离散傅里叶变换求数组周期
GradScaler MaxClipGradScaler
KaFormer个人笔记整理
数据中台:中台实践与总结
Leetcode -- wrong set
数据中台:数据治理概述
Idea another line shortcut
【LeetCode】415. 字符串相加
MySQL - SQL statement
How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
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
A tip to read on Medium for free
How does the tunnel mobile inspection track robot monitor 24 hours to ensure the safety of tunnel construction?