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

@ayepi/cache

v0.6.0

Published

Response-caching middleware for @ayepi/core — keyed by request + a dev-defined vary, with time (ttl/stale-while-revalidate) and space (maxBytes/maxEntries) bounds

Readme

@ayepi/cache

Response-caching middleware for @ayepi/core. It keys a response by the request (method + path + query, plus an optional dev-defined vary — e.g. the authenticated user), and on a hit replays the stored response without running the handler. Entries live in memory for a bounded time (ttl, with optional stale-while-revalidate) and a bounded space (maxBytes / maxEntryBytes / maxEntries, LRU-evicted).

pnpm add @ayepi/cache @ayepi/core zod

It ships as a def / impl split (like @ayepi/rate):

  • @ayepi/cache (frontend-safe) exports cache(opts?), a middleware def factory, plus the standalone memoryCache / cacheKey / cacheHeaders / isCacheableResult primitives. A spec importing only this entry is safe to bundle for the frontend.
  • @ayepi/cache/server augments cache with .server(def, opts), which binds the policy (ttl, vary, bounds, …). Bind the pair with implement(api).middleware(...).

Quick start

// shared.ts — frontend-safe
import { cache } from '@ayepi/cache';
const cached = cache({ requires: [auth] });          // ctx.user typed in `vary`/`key`/`skip`
const api = spec({ endpoints: { ...cached.group({ report: { method: 'GET', response: Report } }) } });

// server.ts — bind the policy
import { cache } from '@ayepi/cache/server';
implement(api)
  .middleware(auth, authImpl)
  .middleware(cache.server(cached, {
    ttl: 30_000,                       // fresh for 30s
    staleWhileRevalidate: 60_000,      // then serve stale up to 60s more while refreshing
    vary: (io) => io.ctx.user.id,      // per-user cache
    maxBytes: 64 * 1024 * 1024,        // bound the memory it can use
  }))
  .handlers({ report: ({ user }) => buildReport(user.id) });

The first request runs the handler and stores the JSON response (X-Cache: MISS); repeats within ttl are replayed (X-Cache: HIT, with Age / Cache-Control) — over HTTP and (as a result frame) over WebSocket. Place cache last in a chain (closest to the handler) so auth / rateLimit / telemetry still run on a hit.

Controls

  • Time — ttl (freshness) and staleWhileRevalidate (serve-stale grace; the stale response goes out immediately while a single background refresh updates the entry).
  • Space — the default memoryCache is an LRU store bounded by maxBytes (total), maxEntryBytes (per response — larger ones aren't cached), and maxEntries (count).
  • Key — method + path + query + body + vary by default (the body is canonicalized, so property order doesn't matter); key(io) overrides it entirely. Works over HTTP and WebSocket (where the call args replace query+body). Only the endpoint's declared methods (default ['GET']) are cached; everything else passes through.
  • Big keys — hash shrinks the key to a short store key (hash: hashKey, or a crypto digest); checkKey (on by default when hashing) keeps + verifies the full key so a collision misses safely.
  • Per-response — shouldCache(io, result), or from the handler via io.ctx.cache.noStore() / io.ctx.cache.ttl(ms).
  • Request Cache-Control — no-store bypasses; no-cache revalidates.
  • Invalidation — hold the store you pass in and call store.delete(cacheKey(...)), store.clear(), or store.invalidate(meta => …) after a mutation.
  • Fail-open — if any cache step throws (the store, key derivation, hashing), the request falls through to the handler as if uncached; the endpoint never errors because of caching. Pass onError(err, phase) to observe those swallowed errors (off by default).

What is cached

Single-response (response:) endpoints returning a JSON body with a 2xx status, over HTTP or WebSocket, for the configured methods. Streams, multi-status (responses:) results, downstream short-circuit Responses, empty (204) bodies, and multipart/file-upload requests pass through uncached. Replays are emitted at status 200.

See ayepi-cache.md for the full reference, and examples/09-cache for a runnable demo.

For AI coding agents

This package ships dense, machine-oriented reference docs written for AI coding agents (Claude Code, Cursor, and the like) to understand and drive the package — point your agent at them:

They ship with this package and also live in the repo.