Cache
The Cache
technique can reduce your HDD read from putting common data into RAM disk.
Using node-cache
Simple KV Storage implementations
yarn add node-cache
server.ts
import "dotenv/config";
import NodeCache from 'node-cache'
const myCache = new NodeCache({
checkperiod: 60 * 10 ,// In seconds, check every 10 mins
});
// Skipped...
app.get("/add", function (req: Request, res: Response) {
let count: number | undefined = myCache.get("count");
if(count === undefined){
count = 1;
}
else{ // Found, make Inc
count += 1
}
myCache.set("count", count)
// (Optional) Set a time out time by 5 mins
// myCache.set("count", count, 60 * 5)
res.json({
currentCount: count
});
});