21 lines
503 B
TypeScript
21 lines
503 B
TypeScript
import fp from 'fastify-plugin'
|
|
import Redis from 'ioredis'
|
|
|
|
declare module 'fastify' {
|
|
interface FastifyInstance {
|
|
redis: Redis
|
|
}
|
|
}
|
|
|
|
export const redisPlugin = fp(async (app) => {
|
|
const redisUrl = process.env.REDIS_URL ?? 'redis://localhost:6379'
|
|
const keyPrefix = process.env.REDIS_KEY_PREFIX ? `${process.env.REDIS_KEY_PREFIX}:` : ''
|
|
const redis = new Redis(redisUrl, { keyPrefix })
|
|
|
|
app.decorate('redis', redis)
|
|
|
|
app.addHook('onClose', async () => {
|
|
await redis.quit()
|
|
})
|
|
})
|