diff --git a/src/cache.ts b/src/cache.ts index bef0d8e..f21306a 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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; } } diff --git a/src/index.ts b/src/index.ts index 97b0ae6..fdc3535 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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);