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 🙏

© 2025 – Pkg Stats / Ryan Hefner

express-middleware-cache-redis

v1.1.0

Published

Express middleware for caching requests using Redis

Downloads

21

Readme

Express Middleware Cache Redis

Boost your Express.js application's performance with effortless Redis caching.

express-middleware-cache-redis is a lightweight, flexible middleware wrapper that simplifies caching HTTP responses. It intercepts requests to serve cached data instantly and provides easy-to-use tools for invalidating cache when your data changes.

🚀 Features

  • Easy Integration: Plug-and-play middleware for any Express route.
  • Flexible Connection: Connect using a Redis URL string OR pass your existing ioredis client instance.
  • Smart Caching: Automatically caches JSON responses based on the request method and URL.
  • Pattern-Based Clearing: Easily clear groups of cached routes (e.g., all /api/users endpoints) with a simple prefix.
  • High Performance: Built on top of ioredis for robust and fast Redis interactions.

📦 Installation

npm install express-middleware-cache-redis

Make sure you have redis installed and running on your machine or server.

🛠 Usage

1. Initialize the Middleware

You can initialize the package in two ways:

Option A: Using a Connection String Great for quick setups or when you want the package to handle the connection.

const express = require('express');
const expressRedisCache = require('express-middleware-cache-redis');

const app = express();

// Connect to local Redis
const { checkCacheMiddleware, clearCacheMiddleware } = expressRedisCache('redis://localhost:6379');

Option B: Using an Existing Client Perfect if your app already uses ioredis and you want to share the connection.

const Redis = require('ioredis');
const expressRedisCache = require('express-middleware-cache-redis');

const redisClient = new Redis(); // Your existing client
const { checkCacheMiddleware, clearCacheMiddleware } = expressRedisCache(redisClient);

2. Caching Responses (GET Requests)

Use checkCacheMiddleware on routes where you want to cache the output. The first time the route is hit, the response is saved to Redis. Subsequent requests are served instantly from the cache.

app.get('/api/products', checkCacheMiddleware, async (req, res) => {
  // Heavy database operation...
  const products = await Product.find(); 
  
  // This JSON response will be cached automatically!
  res.json(products);
});

3. Clearing Cache (POST/PUT/DELETE Requests)

When you add or modify data, you need to clear the old cache so users see the changes. Use clearCacheMiddleware and provide a path prefix to match.

Example: If you have cached data at:

  • GET /api/products
  • GET /api/products/123

You can clear ALL of them by targeting the /api/products prefix:

app.post('/api/products', clearCacheMiddleware('/api/products'), async (req, res) => {
  const newProduct = await Product.create(req.body);
  
  // The cache for anything starting with "/api/products" is now cleared.
  res.json(newProduct);
});

📝 API Reference

expressRedisCache(clientOrUrl)

  • clientOrUrl: Can be a Redis connection string (e.g., 'redis://...') OR an ioredis client instance.
  • Returns: An object containing { checkCacheMiddleware, clearCacheMiddleware, redis }.

checkCacheMiddleware(req, res, next)

  • Middleware that checks if a cache exists for the current route.
  • If found -> sends cached response.
  • If not found -> proceeds to your handler and caches the result.

clearCacheMiddleware(prefix)

  • prefix: A string representing the URL path to clear.
  • Behavior: Scans Redis for all keys containing this prefix and deletes them.
  • Example: clearCacheMiddleware('/api/users') will remove cache for GET-/api/users, GET-/api/users/active, etc.

📄 License

ISC