Add cacheExpiresAt to the response of api

pull/2741/head
Jérémy Rialland 2023-06-20 11:09:12 +02:00 committed by Jérémy Rialland
parent 32a5168a43
commit c93a558e0c
1 changed files with 4 additions and 2 deletions

View File

@ -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
)