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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@b9g/cache-redis

v0.1.5

Published

Redis cache adapter for Shovel cache system

Readme

@b9g/cache-redis

Redis cache adapter for Shovel's universal cache system.

Features

  • HTTP-aware caching: Stores complete HTTP responses with headers and status codes
  • TTL support: Configurable time-to-live for cache entries
  • Size limits: Configurable maximum entry size to prevent memory issues
  • Connection pooling: Uses the official Redis client with connection management
  • Error resilience: Graceful handling of Redis connection issues

Installation

bun install @b9g/cache-redis

Usage

Basic Usage

import {CustomCacheStorage} from "@b9g/cache";
import {RedisCache} from "@b9g/cache-redis";

// Create cache storage with Redis backend
const cacheStorage = new CustomCacheStorage((name: string) => 
  new RedisCache(name, {
    redis: {
      url: "redis://localhost:6379"
    },
    defaultTTL: 3600, // 1 hour
    prefix: "myapp"
  })
);

// Use the cache
const cache = await cacheStorage.open("pages");
await cache.put(request, response);
const cached = await cache.match(request);

Direct Cache Usage

import {createCache} from "@b9g/cache-redis";

// Create a single Redis cache instance
const cache = createCache({
  name: "my-cache",
  redis: {
    url: process.env.REDIS_URL || "redis://localhost:6379",
    password: process.env.REDIS_PASSWORD
  },
  defaultTTL: 3600,
  maxEntrySize: 5 * 1024 * 1024 // 5MB max per entry
});

await cache.put(request, response);
const cached = await cache.match(request);

Configuration Options

RedisCacheOptions

  • redis?: RedisClientOptions - Redis connection configuration
  • prefix?: string - Key prefix for Redis keys (default: "cache")
  • defaultTTL?: number - Default TTL in seconds (0 = no expiration)
  • maxEntrySize?: number - Maximum cache entry size in bytes (default: 10MB)

Redis Connection Options

The redis option accepts all standard Redis client options:

{
  redis: {
    url: "redis://localhost:6379",
    password: "your-password",
    database: 0,
    connectTimeout: 10000,
    lazyConnect: true
  }
}

Environment Variables

Common Redis configuration via environment variables:

  • REDIS_URL - Complete Redis connection URL
  • REDIS_HOST - Redis hostname
  • REDIS_PORT - Redis port
  • REDIS_PASSWORD - Redis password
  • REDIS_DB - Redis database number

Performance Considerations

Entry Size Limits

Large responses are automatically rejected to prevent memory issues:

const cache = new RedisCache("large-files", {
  maxEntrySize: 1024 * 1024 // 1MB limit
});

TTL Configuration

Configure TTL based on your caching strategy:

// Short-lived API responses
const apiCache = new RedisCache("api", {
  defaultTTL: 300 // 5 minutes
});

// Long-lived static assets
const staticCache = new RedisCache("static", {
  defaultTTL: 86400 // 24 hours
});

// Permanent cache (manual invalidation)
const permanentCache = new RedisCache("permanent", {
  defaultTTL: 0 // No expiration
});

Connection Pooling

The Redis client automatically manages connection pooling. For high-traffic applications, consider tuning connection settings:

{
  redis: {
    socket: {
      connectTimeout: 10000,
      keepAlive: true,
      noDelay: true
    },
    isolationPoolOptions: {
      min: 2,
      max: 10
    }
  }
}

Error Handling

The Redis cache gracefully handles connection issues:

  • Failed connections return undefined for cache misses
  • Connection errors are logged but don't crash the application
  • Automatic reconnection when Redis becomes available

Cache Statistics

Get insights into cache performance:

const cache = new RedisCache("my-cache");
const stats = await cache.getStats();

console.log({
  connected: stats.connected,
  keyCount: stats.keyCount,
  totalSize: stats.totalSize,
  prefix: stats.prefix
});

Exports

Classes

  • RedisCache - Redis-backed cache implementation (extends Cache from @b9g/cache)

Types

  • RedisCacheOptions - Configuration options for RedisCache

Shovel Integration

This cache adapter is designed to work seamlessly with Shovel's cache-first architecture:

  • Platform agnostic: Works with any Shovel platform (Bun, Node.js, Cloudflare)
  • HTTP-aware: Preserves response headers and status codes
  • ServiceWorker compatible: Implements the standard Cache API interface

For more information about Shovel's caching system, see the @b9g/cache documentation.