mon-entreprise/api/source/redis-cache.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

import Router from '@koa/router'
import IORedis from 'ioredis'
import IORedisMock from 'ioredis-mock'
import { koaBody } from 'koa-body'
const Redis = IORedis.default
const RedisMock = IORedisMock.default
// cache expires in 2 hours
const CACHE_EXPIRE = 2 * 60 * 60
const redis =
process.env.NODE_ENV === 'production' && process.env.SCALINGO_REDIS_URL
? new Redis(process.env.SCALINGO_REDIS_URL, {
enableOfflineQueue: false,
2023-06-15 14:29:29 +00:00
keyPrefix: 'cache',
})
: new RedisMock()
export const redisCacheMiddleware = () => {
const router = new Router()
router.post('/evaluate', koaBody(), async (ctx, next) => {
if (!redis || !ctx.request.body) {
await next()
return
}
const cacheKey = JSON.stringify(ctx.request.body)
const cachedResponse = await redis.get(cacheKey)
if (cachedResponse) {
ctx.body = JSON.parse(cachedResponse) as unknown
return
}
await next()
if (ctx.status === 200) {
await redis.set(
cacheKey,
JSON.stringify({ responseCachedAt: Date.now(), ...ctx.body }),
'EX',
CACHE_EXPIRE
)
}
})
return router.routes()
}