@heyhru/server-plugin-redis
v0.2.2
Published
Redis client wrapper powered by ioredis
Readme
@heyhru/server-plugin-redis
Fastify plugin wrapping ioredis. Registers a shared Redis client on the Fastify instance with automatic lifecycle management.
Install
pnpm add @heyhru/server-plugin-redisUsage
import Fastify from "fastify";
import { redisPlugin } from "@heyhru/server-plugin-redis";
const app = Fastify();
await app.register(redisPlugin, { url: "redis://:password@host:6379/0" });
// Access via fastify.redis or req.server.redis
app.get("/", async (req) => {
const redis = req.server.redis; // Redis | null
if (redis) {
await redis.set("key", "value", "EX", 60);
return { cached: await redis.get("key") };
}
return { cached: null };
});When url is omitted or empty, the plugin decorates fastify.redis with null — callers should fall back to the database.
API
redisPlugin
Fastify plugin (wrapped with fastify-plugin).
Options:
| Option | Type | Required | Description |
| ------ | -------- | -------- | -------------------- |
| url | string | No | Redis connection URL |
Decorators:
| Name | Type | Description |
| ------- | -------------- | ------------------------------------------ |
| redis | Redis | null | Shared ioredis client, null when disabled |
The client is automatically closed via Fastify's onClose hook.
