From 53525dcd52cf5c797d5e1cd9ee1d0e588193d192 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 4 Mar 2026 13:36:35 +0300 Subject: [PATCH] feat: add Redis plugin Made-with: Cursor --- src/plugins/redis.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/plugins/redis.ts diff --git a/src/plugins/redis.ts b/src/plugins/redis.ts new file mode 100644 index 0000000..3760029 --- /dev/null +++ b/src/plugins/redis.ts @@ -0,0 +1,32 @@ +import { FastifyInstance, FastifyPluginAsync } from 'fastify'; +import { Redis } from 'ioredis'; +import fp from 'fastify-plugin'; +import { env } from '../config/env.js'; + +declare module 'fastify' { + interface FastifyInstance { + redis: Redis; + } +} + +const redisPlugin: FastifyPluginAsync = async (app: FastifyInstance) => { + const redis = new Redis(env.REDIS_URL, { + maxRetriesPerRequest: 3, + retryStrategy(times: number) { + const delay = Math.min(times * 100, 3000); + return delay; + }, + }); + + redis.on('error', (err: Error) => { + app.log.error({ err }, 'Redis connection error'); + }); + + app.decorate('redis', redis); + + app.addHook('onClose', async () => { + await redis.quit(); + }); +}; + +export default fp(redisPlugin, { name: 'redis' });