Merge pull request #9 from LeRoid-hub/8-custom-cache-lifetime

cusom Cache lifetime
This commit is contained in:
LeRoid-hub 2023-12-23 21:49:36 +01:00 committed by GitHub
commit 5e9a6b12d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

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

View File

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