cusom Cache lifetime

This commit is contained in:
Jan Barfuss 2023-12-23 21:48:49 +01:00
parent 519b9ea6cc
commit df197e4cb8
2 changed files with 9 additions and 4 deletions

View File

@ -23,10 +23,11 @@ class SiteCache {
return this.cachedData;
}
set(key: string, data: any) {
set(key: string, data: any, lifeTime: number = 1000 * 60 * 30) {
this.cachedData = data;
this.lastUsed = new Date();
this.key = key;
this.lifeTime = lifeTime;
}
}
@ -46,10 +47,10 @@ export default class Cache {
return null;
}
set(key: string, data: any) {
set(key: string, data: any, lifeTime: number = 1000 * 60 * 30) {
for (let i = 0; i < this.cache.length; i++) {
if (this.cache[i].key === key) {
this.cache[i].set(key, data);
this.cache[i].set(key, data, lifeTime);
return;
}
}

View File

@ -40,7 +40,8 @@ app.get("/api/bl/:Bundesland", (req: Request, res: Response) => {
return res.send("Invalid request");
}
stripedData = stripCampus(data);
cache.set("BL: "+req.params.Bundesland, stripedData);
const lifeTime = 1000 * 60 * 60 * 24 * 7;
cache.set("BL: "+req.params.Bundesland, stripedData, lifeTime);
res.send(stripedData);
});
}
@ -59,13 +60,16 @@ app.get("/api/:Location/:Mensa?", (req: Request, res: Response) => {
}
fetch(url).then((data) => {
let stripedData = null;
let lifeTime = 1000 * 30;
if (data === null) {
return res.send("Invalid request");
}
if (req.params.Mensa !== undefined) {
stripedData = stripMensa(data);
lifeTime = 1000 * 60 * 60 * 24;
}else {
stripedData = stripCampus(data);
lifeTime = 1000 * 60 * 30;
}
cache.set(req.params.Mensa ?? req.params.Location, stripedData);