-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.v2.js
49 lines (36 loc) · 1.05 KB
/
redis.v2.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
const express = require('express');
const { exists, incrby ,get,set, run, disconnect, setnx } = 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 setnx(keyName,0);
}
let amountSold =await get(keyName);
amountSold = await incrby(keyName,amount);
if (+amountSold > IventoryNum){
console.log("Not enough amount");
return res.json({
status:"error",
msg:'Not enough',
time
})
}
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");
})