-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.v1.js
50 lines (37 loc) · 1.09 KB
/
redis.v1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const express = require('express');
const client = require('./models/init.redis');
const { exists, incrby ,get,set, run, disconnect } = require('./models/model.redis');
const app = express();
app.get('/order',async(req,res)=>{
const time = new Date().getTime();
console.log("Time request:::",time);
const IventoryNum =10;
const keyName = 'iPhone14';
const amount =1;
const getKey = await exists(keyName);
if(!getKey){
await set(keyName,0);
}
let amountSold =await get(keyName);
if (+amountSold+amount > IventoryNum){
console.log("Not enough amount");
return res.json({
status:"error",
msg:'Not enough',
time
})
}
amountSold = await incrby(keyName,amount);
console.log("After sold:", amountSold);
if (+amountSold> IventoryNum){
await set("amountOver",amountSold-IventoryNum);
}
return res.json({
status:"success",
msg:'Ok',
time
})
})
app.listen(3000,()=>{
console.log("The server running at http://localhost:3000 is listening on");
})