From c93a558e0c436a9b0f27c8f703e30fb8d36328e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Rialland?= Date: Tue, 20 Jun 2023 11:09:12 +0200 Subject: [PATCH] Add cacheExpiresAt to the response of api --- api/source/redis-cache.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/api/source/redis-cache.ts b/api/source/redis-cache.ts index 5122a915e..6bd31f2bc 100644 --- a/api/source/redis-cache.ts +++ b/api/source/redis-cache.ts @@ -8,7 +8,7 @@ import { koaBody } from 'koa-body' const Redis = IORedis.default const RedisMock = IORedisMock.default -// cache expires in 12 hours +// cache expires in 12 hours (in seconds) const CACHE_EXPIRE = 12 * 60 * 60 const redis = @@ -43,9 +43,11 @@ export const redisCacheMiddleware = () => { await next() if (ctx.status === 200) { + const responseCachedAt = Date.now() + const cacheExpiresAt = responseCachedAt + CACHE_EXPIRE * 1000 await redis.set( cacheKey, - JSON.stringify({ responseCachedAt: Date.now(), ...ctx.body }), + JSON.stringify({ responseCachedAt, cacheExpiresAt, ...ctx.body }), 'EX', CACHE_EXPIRE )