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

@g14o/cache

v0.6.0

Published

Framework-agnostic caching with pluggable stores (memory, Upstash, Redis).

Readme

@g14o/cache

Documentation: docs.g14o.dev/packages/cache

Framework-agnostic caching with pluggable stores (memory, Upstash, node-redis/ioredis). In-memory fallbacks apply automatically in development, test, and static build phases.

Install

pnpm add @g14o/cache @upstash/redis

Install only the peer(s) matching your chosen store:

| Store | Peer dependency | |-------|-----------------| | Upstash | @upstash/redis | | node-redis | redis | | ioredis | ioredis |

Setup

Create an app-owned client in lib/cache.ts:

import { createCache } from "@g14o/cache";
import { upstashStore } from "@g14o/cache/upstash";

export const { withCache, invalidateCache, invalidateCacheKey } = createCache({
  store: upstashStore({
    url: process.env.UPSTASH_REDIS_REST_URL!,
    token: process.env.UPSTASH_REDIS_REST_TOKEN!,
  }),
  verbose: true,
});

Other stores

import { memoryStore } from "@g14o/cache/memory";
import { redisStore } from "@g14o/cache/redis";

createCache({ store: memoryStore() });
createCache({ store: redisStore(redisClient) });

Legacy redis option

The redis option still works and wraps upstashStore internally. Prefer store: upstashStore(...) for new projects.

Custom store from raw KV primitives

import { createStore } from "@g14o/cache";

const store = createStore({
  async read(key) { /* return string | null */ },
  async write(key, value, ttlSeconds) { /* persist string */ },
  async remove(...keys) { /* return deleted count */ },
  async list(pattern) { /* return matching keys */ },
});

By default, createStore JSON-serializes values. undefined (and other values that JSON.stringify cannot encode) are stored as a string sentinel and round-trip back to undefined. Pass custom serialize / deserialize if needed; serializers must return a string — if a custom serializer returns undefined, createStore coerces it to the undefined sentinel before write().

createStore(primitives, {
  serialize: (value) => { /* must return string */ },
  deserialize: (raw) => { /* parse raw string */ },
  prefix: "app",
});

Bare null and undefined return values from cached functions are not cached (CacheStore.get uses null for missing keys). Use a Result or domain value when an intentional empty answer should be cached.

Examples

Wrap a server function with withCache

withCache accepts any async function. Result-shaped returns ({ ok: true | false }) cache successes by default; opt in to caching failures with cacheFailures: true.

export const getUsersCached = withCache(getUsers, {
  ttl: "medium",
  prefix: "users",
});

Stale-while-revalidate

withCache(getUsers, {
  ttl: "medium",
  staleWhileRevalidate: 60, // serve stale for 60s while refreshing in background
});

Negative caching (opt-in)

withCache(getUser, {
  cacheFailures: true,
});

Custom failure TTL:

withCache(getUser, {
  cacheFailures: { enabled: true, ttl: "medium" },
});

Invalidate after a mutation

await invalidateCacheKey(createEntityCacheKey("user", id));
await invalidateCache("*", { prefix: "users" });

Import paths

| Use case | Import | |----------|--------| | Cache factory and helpers | @g14o/cache | | Memory store | @g14o/cache/memory | | Upstash store | @g14o/cache/upstash | | node-redis / ioredis store | @g14o/cache/redis | | Shared types | @g14o/cache/types | | Redis / env helpers | @g14o/cache/config |

Build vs runtime

When no store is configured, development/test/build phases use in-memory cache automatically. Production requires an explicit store (or legacy redis).

See inMemoryDuringBuild and isBuildLikePhase() from @g14o/cache/config.