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

@karmsakha/next-vps-cache

v1.0.0

Published

Redis-first cacheHandlers utility for self-hosted Next.js 16+ with hardened serialization and fallback memory cache.

Downloads

218

Readme

@karmsakha/next-vps-cache

CI npm version license coverage

Redis-first cache utilities for self-hosted Next.js 16+ (cacheHandlers) with:

  • hardened stream-safe serialization/deserialization
  • runtime shape validation with repair hints
  • tiered Redis + memory fallback behavior
  • structured metrics/log hooks for observability

Why this exists

Self-hosted Next.js deployments can fail hard when custom cache payloads drift from expected stream/binary shapes. This package prevents response-pipeline crashes by validating and normalizing cache entries before they are returned to Next internals.

Install

npm install @karmsakha/next-vps-cache

Compatibility Matrix

| Layer | Supported | | --- | --- | | Next.js | >=16.1.x | | Node.js | >=18.17 | | Redis | >=6 (tested with Redis 7) | | Runtime | Node.js self-hosted VPS (non-edge) |

Quick Start (Next.js 16+ cacheHandlers)

next.config.ts

import type { NextConfig } from "next";

const nextConfig: NextConfig = {
  cacheHandlers: {
    default: require.resolve("./cache-handlers/default"),
    remote: require.resolve("./cache-handlers/remote"),
  },
};

export default nextConfig;

cache-handlers/default.ts

import {
  createTieredVpsCacheHandler,
  createConsoleLogger,
} from "@karmsakha/next-vps-cache";

const logger = createConsoleLogger({ level: "info", prefix: "next-cache" });

const handler = createTieredVpsCacheHandler({
  redis: {
    url: process.env.REDIS_URL,
    keyPrefix: "myapp:next:",
    logger,
  },
  fallback: {
    maxItems: 5000,
    maxTotalBytes: 256 * 1024 * 1024,
    logger,
  },
  logger,
});

export default handler;

cache-handlers/remote.ts

import {
  createRedisVpsCacheHandler,
  createConsoleLogger,
} from "@karmsakha/next-vps-cache";

export default createRedisVpsCacheHandler({
  url: process.env.REDIS_URL,
  keyPrefix: "myapp:next:",
  logger: createConsoleLogger({ level: "info", prefix: "next-cache-remote" }),
});

API (stable for v1.x)

createRedisVpsCacheHandler(options)

Returns a Next-compatible handler backed by Redis (ioredis).

createMemoryFallbackCacheHandler(options?)

Returns a bounded in-memory handler for local fallback.

createTieredVpsCacheHandler(options)

Returns a tiered handler: Redis primary + memory fallback.

serializeCacheEntry(entry) / deserializeCacheEntry(raw)

Stream-safe serializer/deserializer for cache entry persistence.

validateCacheEntryShape(entry)

Validates and optionally repairs known malformed shapes.

Types

  • RedisVpsCacheOptions
  • FallbackOptions
  • SerializationOptions
  • VpsCacheMetricsEvent
  • VpsCacheLogger

Migration Guide (deprecated custom handlers -> cacheHandlers)

  1. Move old custom-cache logic out of deprecated adapters.
  2. Create explicit cache-handlers/default.ts and cache-handlers/remote.ts.
  3. Use createTieredVpsCacheHandler for default route cache and resilience.
  4. Keep createRedisVpsCacheHandler for remote-only use where required.
  5. Enable observability via logger.emit(event) and track:
    • hit, miss, fallback-hit, deserialize-fail, redis-timeout

Troubleshooting Matrix

| Symptom | Likely cause | What to check | Action | | --- | --- | --- | --- | | Opaque stream/pipe runtime failure | malformed cached value shape | deserialize-fail / deserialize-repair events | verify serializer usage, clear bad keys | | High fallback ratio | Redis connectivity/timeouts | redis-timeout events, Redis latency | increase timeout budget, inspect network | | High miss rate despite cache writes | key prefix mismatch or expiry drift | keyPrefix config, expire/revalidate fields | align keyPrefix and ttl logic | | Slow TTFB during incident | unbounded in-memory fallback | fallback maxItems / maxTotalBytes | set explicit bounds and monitor evictions |

Observability

Events emitted through logger.emit(event) include:

  • hit
  • miss
  • stale
  • set
  • fallback-hit
  • redis-timeout
  • deserialize-repair
  • deserialize-fail

Benchmark Baseline

Use the included script with autocannon:

npm run benchmark:baseline -- --url http://127.0.0.1:3000 --connections 50 --duration 30

It writes a JSON summary to ./reports/ by default.

Development

npm run typecheck
npm run test
npm run test:coverage
npm run build

Security and Support