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

memcachify

v1.1.0

Published

Production-ready lightweight in-memory caching middleware for Node.js (Express + Fastify) & client side caching.

Readme

Memcachify ⚡

Production-ready, ultra-fast, zero-dependency caching for Node.js.

Memcachify is a powerful, lightweight caching solution designed for high-performance Express and Fastify APIs. It features a custom LRU engine, background revalidation (SWR), and intelligent stampede prevention.

NPM Version License: MIT Tests


🔥 Key Features

  • 🏎️ Ultra-Fast LRU Store: Custom $O(1)$ implementation with item count and byte-size limits.
  • 🔄 Stale-While-Revalidate (SWR): Serve stale data instantly while fetching fresh data in the background.
  • 🛡️ Stampede Prevention: Automatic promise coalescing to prevent "Thundering Herd" effects on your database.
  • 🌐 HTTP Protocol Compliant: Full support for Vary, ETag, If-None-Match, and Cache-Control headers.
  • 📈 Observability: Built-in stats tracking (hit rate, evictions) and event hooks (hit, miss, evict).
  • 🏷️ Tag-based Invalidation: Invalidate multiple entries at once using custom tags.
  • 📦 Zero Dependencies: Core logic is 100% dependency-free for maximum security and minimal footprint.
  • 🖥️ Universal Support: Works in Node.js, browsers (localStorage persistence), and Edge workers.

🚀 Installation

npm install memcachify

📖 Usage Examples

1. Express Middleware

The easiest way to cache your routes. Memcachify automatically handles headers and status codes.

import express from 'express';
import { cache, invalidate } from 'memcachify';

const app = express();

// Cache for 60 seconds
app.get('/api/products', cache({ ttl: 60 }), (req, res) => {
  res.json({ products: [...] });
});

// Manual invalidation
app.post('/api/products', (req, res) => {
  invalidate((key) => key.includes('/api/products'));
  res.status(201).send('Created');
});

2. Advanced: Stale-While-Revalidate (SWR)

Keep your API responsive even when the cache expires. SWR serves the stale data while refreshing the cache in the background.

app.get('/api/trending', cache({
  ttl: 30,
  staleWhileRevalidate: 300, // Serve stale up to 5 mins
  revalidate: async (req) => {
    const data = await db.getTrending();
    return { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: data };
  }
}), (req, res) => {
  // Initial request handler
});

3. Fastify Middleware

import Fastify from 'fastify';
import { fastifyCache, InMemoryStore } from 'memcachify';

const fastify = Fastify();
const store = new InMemoryStore({ maxItems: 5000 });

fastify.register(fastifyCache({ store, ttl: 60 }));

fastify.get('/users', async (req, reply) => {
  return { users: [] };
});

4. Browser / Client Cache

MEMcachify comes with a separate client-side bundle that supports localStorage persistence.

import { ClientCache } from 'memcachify/client';

const clientCache = new ClientCache({ 
  ttlMs: 60_000, 
  storageKeyPrefix: 'myapp:' 
});

clientCache.set('user_profile', { name: 'Alice' });
const user = clientCache.get('user_profile');

🛠️ Advanced Configuration

Store Options

Configure the InMemoryStore with strict limits to prevent memory leaks.

const store = new InMemoryStore({
  maxItems: 10000,           // Max number of entries
  maxSize: 100 * 1024 * 1024, // 100MB byte limit
  defaultTTL: 300,            // 5 minutes
  cleanupIntervalMs: 60000,   // Frequency of expired key cleanup
  logger: console             // Pino-compatible logger
});

// Observability
store.on('hit', (key) => console.log(`Cache Hit: ${key}`));
store.on('evict', (key) => console.warn(`Cache Evicted: ${key}`));

console.log(store.stats()); // { hits, misses, evictions, hitRate, ... }

Invalidation

store.invalidateByTag('category:electronics'); // Invalidate everything tagged
store.invalidate((key) => key.startsWith('/api/v1')); // Predicate-based
store.clear(); // Nuclear option

🤝 Collaboration & Support

Memcachify is open-source and welcoming to contributions!

  • 🐛 Issues: Found a bug or have a feature request? Open an issue.
  • 🙋 Collaboration: Want to help out? Feel free to fork the repo and submit a PR. See our Contribution Guide for more details.
  • ⭐ Support: If you find this project useful, please give it a star on GitHub!

📜 License

MIT © Darshan Battula