From a4641b848054d0b791be9b655890161cc8b0823d Mon Sep 17 00:00:00 2001 From: nymkappa Date: Mon, 11 Jul 2022 09:53:32 +0200 Subject: [PATCH] Set /block API cache duration according to block age --- backend/src/routes.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/backend/src/routes.ts b/backend/src/routes.ts index aeda734e1..0bd3aa7cf 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -761,7 +761,19 @@ class Routes { public async getBlock(req: Request, res: Response) { try { const block = await blocks.$getBlock(req.params.hash); - res.setHeader('Expires', new Date(Date.now() + 1000 * 600).toUTCString()); + + const blockAge = new Date().getTime() / 1000 - block.timestamp; + const day = 24 * 3600; + let cacheDuration; + if (blockAge > 365 * day) { + cacheDuration = 30 * day; + } else if (blockAge > 30 * day) { + cacheDuration = 10 * day; + } else { + cacheDuration = 600 + } + + res.setHeader('Expires', new Date(Date.now() + 1000 * cacheDuration).toUTCString()); res.json(block); } catch (e) { res.status(500).send(e instanceof Error ? e.message : e);