当前位置:网站首页>[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 !");
}
边栏推荐
- 数据中台:国内大厂中台建设架构集锦
- OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集
- 【牛客】把字符串转换成整数
- 【使用 PicGo+腾讯云对象存储COS 作为图床】
- eBanb B1手环刷固件异常中断处理
- “不平凡的代理初始值设定不受支持”,出现的原因及解决方法
- 关于 GIN 的路由树
- [MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
- 4274. suffix expression
- [quantitative investment] discrete Fourier transform to calculate array period
猜你喜欢

嵌入式 | 硬件转软件的几条建议

A tip to read on Medium for free

MySQL | store notes of Master Kong MySQL from introduction to advanced

Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems

JS to find and update the specified value in the object through the key

The form image uploaded in chorme cannot view the binary image information of the request body

Huawei Router: IPSec Technology

Qingcloud based "real estate integration" cloud solution

uniapp 开发多端项目如何配置环境变量以及区分环境打包

Spark - LeftOuterJoin 结果条数与左表条数不一致
随机推荐
One article explains in detail | those things about growth
Squid代理服务器应用
Determination of monocular and binocular 3D coordinates
【LeetCode】415. 字符串相加
4274. suffix expression
普通人没有学历,自学编程可以月入过万吗?
【E325: ATTENTION】vim编辑时报错
听说你还在花钱从网上买 PPT 模板?
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
MySQL | view notes on Master Kong MySQL from introduction to advanced
216. 组合总和 III-枚举法
MySQL | 视图《康师傅MySQL从入门到高级》笔记
Huawei Router: GRE Technology
mysql写的代码数据 增删查改等等
leetcode——错误的集合
Change SSH port number
2138. splitting a string into groups of length k
用VNC Viewer的方式远程连接无需显示屏的树莓派
[Niuke] convert string to integer
I heard that you are still spending money to buy ppt templates from the Internet?