@fastify/redis
Redis connection plugin for Fastify — share a Redis client instance across your entire Fastify application via decorators.
@fastify/redis
FastifyRedis connection plugin for Fastify — share a Redis client instance across your entire Fastify application via decorators.
Fit
Bundle (gzip)
40.5 kB
139.1 kB raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Sharing a Redis client across Fastify plugins and route handlers without passing it manually through every function requires Fastify's decorator system — wiring this up correctly with proper lifecycle management is boilerplate.
What It Does
@fastify/redis registers an ioredis client and decorates the Fastify instance with fastify.redis. The client is automatically closed when the server shuts down. Multiple named clients allow connecting to different Redis instances for different purposes.
Installation
npm install @fastify/redisUsage Example
import fastify from 'fastify';
import redis from '@fastify/redis';
const app = fastify();
await app.register(redis, {
host: process.env.REDIS_HOST,
port: 6379,
family: 4,
});
app.get('/cached-data', async (request, reply) => {
const cached = await app.redis.get('data:list');
if (cached) return JSON.parse(cached);
const data = await fetchData();
await app.redis.setex('data:list', 300, JSON.stringify(data));
return data;
});Related packages
Auto-load Fastify plugins from a directory — organize routes and plugins as files and let the framework discover them automatically.
@fastify/cookieFastifyPlugin for Fastify that adds support for reading and setting cookies, including signed cookies.
@fastify/corsFastifyCORS plugin for Fastify — configure cross-origin resource sharing with full support for preflight requests and dynamic origins.
@fastify/helmetFastifySecurity headers plugin for Fastify — sets HTTP headers that protect against common web vulnerabilities like XSS, clickjacking, and MIME sniffing.
@fastify/jwtFastifyJWT authentication plugin for Fastify — sign, verify, and decode tokens with a decorator-based API that integrates with Fastify's request lifecycle.