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

@cachein/core

v0.1.0

Published

Type-safe caching library with in-memory and LRU backends

Downloads

10

Readme

@cachein/core

Lightweight, type-safe caching library with built-in memory and LRU backends.

Features

  • 🔒 Type Safety - Full TypeScript support with Zod schema validation
  • 🚀 Multiple Backends - In-memory and LRU cache implementations
  • 🛡️ Stampede Protection - Built-in request coalescing to prevent cache stampedes
  • ⏱️ TTL Support - Flexible expiration with default and per-key TTLs
  • 🔐 Optional Encryption - Encrypt cached values for sensitive data
  • 🔑 Key Hashing - Optional SHA-256 key hashing for storage optimization
  • 🏗️ Tiered Caching - Combine multiple backends for multi-level caching
  • 📝 Structured Logging - Built-in logging with Pino

Installation

npm install @cachein/core
# or
bun install @cachein/core

Quick Start

import { CacheIn, InMemoryBackend } from "@cachein/core";
import { z } from "zod";

// Create a cache client
const client = new CacheIn(new InMemoryBackend(), {
  defaultTtl: 300000, // 5 minutes
});

// Define a schema
const userSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email(),
});

// Bind a typed cache
const userCache = client.bind("users", userSchema);

// Cache operations
await userCache.set("123", {
  id: 123,
  name: "John Doe",
  email: "[email protected]",
});

const user = await userCache.get("123");

// Wrap pattern with stampede protection
const user2 = await userCache.wrap("456", 60000, async () => {
  // This function only runs if cache miss
  return await fetchUserFromDatabase(456);
});

Backends

InMemoryBackend

Simple Map-based cache with optional TTL support.

import { InMemoryBackend } from "@cachein/core";

const backend = new InMemoryBackend({
  logger: true, // Enable logging
});

LruBackend

Least Recently Used cache with configurable size limits.

import { LruBackend } from "@cachein/core";

const backend = new LruBackend({
  maxSize: 1000, // Maximum number of entries
  logger: true,
});

TieredCacheBackend

Combine multiple backends for multi-level caching.

import { TieredCacheBackend, LruBackend, InMemoryBackend } from "@cachein/core";

const backend = new TieredCacheBackend([
  new LruBackend({ maxSize: 100 }), // L1: Fast, small
  new InMemoryBackend(), // L2: Larger, still fast
]);

Advanced Features

Key Hashing

Enable SHA-256 key hashing to store hashed keys instead of raw text:

const client = new CacheIn(backend, {
  keyHashing: true, // Keys are SHA-256 hashed before storage
});

Value Encryption

Encrypt cached values for sensitive data:

import { CacheIn, InMemoryBackend, createEncryptionKey } from "@cachein/core";

const key = await createEncryptionKey("my-secret-password");

const client = new CacheIn(backend, {
  encryption: { key },
});

Request Coalescing

The wrap() method automatically prevents cache stampedes:

// Multiple concurrent calls for the same key will only execute the function once
const [result1, result2, result3] = await Promise.all([
  userCache.wrap("123", 60000, fetchExpensiveData),
  userCache.wrap("123", 60000, fetchExpensiveData),
  userCache.wrap("123", 60000, fetchExpensiveData),
]);
// fetchExpensiveData is called only once

API Reference

See the main README for complete API documentation.

License

MIT © Jonas