npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@project-karin/redis

v0.5.21

Published

Redis client integration plugin for Karin using ioredis.

Readme

@project-karin/redis

Redis client integration plugin for Karin using ioredis.

Installation

bun add @project-karin/redis ioredis

Overview

The Redis plugin provides:

  • ✅ Redis connection management
  • ✅ Automatic reconnection
  • ✅ Dependency injection for Redis client
  • ✅ Lazy configuration resolution
  • ✅ Graceful shutdown handling

Quick Start

import { RedisPlugin, InjectRedis } from "@project-karin/redis";
import type { Redis } from "ioredis";

// Configure plugin
const app = await KarinFactory.create(adapter, {
  plugins: [
    new RedisPlugin({
      url: "redis://localhost:6379",
    }),
  ],
});

// Use in services
@Service()
class CacheService {
  constructor(@InjectRedis() private redis: Redis) {}

  async set(key: string, value: string, ttl?: number) {
    if (ttl) {
      await this.redis.setex(key, ttl, value);
    } else {
      await this.redis.set(key, value);
    }
  }

  async get(key: string) {
    return this.redis.get(key);
  }

  async del(key: string) {
    return this.redis.del(key);
  }
}

Features

Simple Configuration

new RedisPlugin("redis://localhost:6379")

Advanced Configuration

new RedisPlugin({
  url: "redis://localhost:6379",
  options: {
    password: "secret",
    db: 0,
    retryStrategy: (times) => Math.min(times * 50, 2000),
  },
})

Lazy Configuration

Use with ConfigPlugin:

const config = new ConfigPlugin({
  requiredKeys: ["REDIS_URL"],
});

const app = await KarinFactory.create(adapter, {
  plugins: [
    config,
    new RedisPlugin({
      url: () => config.get("REDIS_URL"),
      options: () => ({
        password: config.get("REDIS_PASSWORD"),
      }),
    }),
  ],
});

Failure Strategy

Control what happens if Redis connection fails:

new RedisPlugin({
  url: "redis://localhost:6379",
  failureStrategy: "warn", // or "fail"
})
  • "fail" (default): Throw error and stop application
  • "warn": Log warning and continue without Redis

Usage Examples

Caching

@Service()
class UsersService {
  constructor(
    @InjectRedis() private redis: Redis,
    @InjectModel(User) private userModel: Model<User>
  ) {}

  async findById(id: string) {
    // Check cache
    const cached = await this.redis.get(`user:${id}`);
    if (cached) {
      return JSON.parse(cached);
    }

    // Fetch from database
    const user = await this.userModel.findById(id);

    // Cache for 1 hour
    await this.redis.setex(`user:${id}`, 3600, JSON.stringify(user));

    return user;
  }
}

Session Storage

@Service()
class SessionService {
  constructor(@InjectRedis() private redis: Redis) {}

  async createSession(userId: string, data: any) {
    const sessionId = crypto.randomUUID();
    await this.redis.setex(
      `session:${sessionId}`,
      86400, // 24 hours
      JSON.stringify({ userId, ...data })
    );
    return sessionId;
  }

  async getSession(sessionId: string) {
    const data = await this.redis.get(`session:${sessionId}`);
    return data ? JSON.parse(data) : null;
  }

  async destroySession(sessionId: string) {
    await this.redis.del(`session:${sessionId}`);
  }
}

Rate Limiting

@Service()
class RateLimiter {
  constructor(@InjectRedis() private redis: Redis) {}

  async checkLimit(key: string, limit: number, window: number): Promise<boolean> {
    const current = await this.redis.incr(key);

    if (current === 1) {
      await this.redis.expire(key, window);
    }

    return current <= limit;
  }
}

@Service()
class RateLimitGuard implements CanActivate {
  constructor(private rateLimiter: RateLimiter) {}

  async canActivate(context: ExecutionContext): Promise<boolean> {
    const request = context.switchToHttp().getRequest();
    const ip = request.headers.get("x-forwarded-for") || "unknown";

    const allowed = await this.rateLimiter.checkLimit(
      `ratelimit:${ip}`,
      100, // 100 requests
      60   // per minute
    );

    if (!allowed) {
      throw new HttpException("Rate limit exceeded", 429);
    }

    return true;
  }
}

API

RedisPlugin Options

type RedisPluginOptions =
  | string  // Simple URL
  | RedisOptions  // ioredis options
  | {
      url?: string | (() => string);
      options?: RedisOptions | (() => RedisOptions);
      failureStrategy?: "fail" | "warn";
      // Default: Automatically detected via environment variables
      serverless?: boolean;
    };

InjectRedis Decorator

@InjectRedis() private redis: Redis

Best Practices

  1. Use caching wisely - Don't cache everything
  2. Set TTLs - Prevent memory bloat
  3. Handle failures - Redis should enhance, not block
  4. Use pipelines - Batch operations for performance
  5. Monitor memory - Use Redis monitoring tools

License

MIT