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

express-smart-cache

v1.0.7

Published

A powerful, lightweight caching middleware for Express.js that seamlessly integrates in-memory and Redis caching with advanced features like automatic cache invalidation, configurable TTL, and performance metrics.

Readme

Express Smart Cache

A lightweight, flexible caching middleware for Express with support for in-memory and Redis-based caching.

🚀 Features

  • In-Memory & Redis Support – Choose between fast local caching or scalable Redis-based caching
  • Flexible Caching Strategies – Customize cache key generation, route exclusions, and method filtering
  • TTL (Time-To-Live) Support – Define how long responses stay cached
  • Automatic Cache Invalidation – Cache expires after the defined TTL
  • Metrics and Logging – Optional performance tracking and error logging
  • Easy Integration – Works seamlessly with any Express.js API

📦 Installation

npm install express-smart-cache

🔧 Basic Usage

Simple In-Memory Caching

const express = require('express');
const CacheMiddleware = require('express-smart-cache');

const app = express();
const cache = new CacheMiddleware({ ttl: 60 }); // Cache for 60 seconds

app.use(cache.middleware());

app.get("/users", (req, res) => {
  res.json({ 
    users: [
      { id: 1, name: "John Doe" },
      { id: 2, name: "Jane Smith" }
    ]
  });
});

app.listen(3000, () => console.log("Server running on port 3000"));

Redis Caching

const express = require('express');
const CacheMiddleware = require('express-smart-cache');

const app = express();
const cache = new CacheMiddleware({
  redis: true,
  redisHost: "your-redis-host",
  redisPort: 6379,
  ttl: 120 // Cache for 2 minutes
});

app.use(cache.middleware());

app.get("/products", (req, res) => {
  res.json({ 
    products: [
      { id: 1, name: "Laptop", price: 999 },
      { id: 2, name: "Smartphone", price: 599 }
    ]
  });
});

app.listen(3000, () => console.log("Server running on port 3000"));

🛠 Advanced Configuration

Comprehensive Options

const cache = new CacheMiddleware({
  // Caching Options
  ttl: 300, // 5 minutes cache duration
  redis: true, // Enable Redis
  redisHost: "127.0.0.1",
  redisPort: 6379,

  // Advanced Features
  excludeRoutes: ['/admin', '/login'], // Routes to skip caching
  allowedMethods: ['GET', 'HEAD'], // Only cache these methods
  cacheControl: true, // Add HTTP cache-control headers
  metrics: true, // Enable performance tracking
  logging: true, // Enable error logging

  // Custom Key Generation
  keyGenerator: (req) => {
    // Custom cache key logic
    return `custom-${req.originalUrl}:${req.method}`;
  }
});

Manually Invalidating Cache

// Clear cache for a specific route
cache.invalidateCache("/users");

// Get current cache metrics
const metrics = cache.getMetrics();
console.log(metrics);
// Outputs: { hits: 10, misses: 5, size: 3 }

🚢 API Middleware Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | ttl | Number | 60 sec | Time-to-live for cached responses | | redis | Boolean | false | Enable Redis-based caching | | redisHost | String | "127.0.0.1" | Redis server host | | redisPort | Number | 6379 | Redis server port | | excludeRoutes | Array | [] | Routes to exclude from caching | | allowedMethods | Array | ['GET'] | HTTP methods to cache | | cacheControl | Boolean | false | Add HTTP cache headers | | metrics | Boolean | false | Track cache performance | | logging | Boolean | false | Enable error logging |