Skip to content

Lightweight HTTP response caching middleware for Express

Notifications You must be signed in to change notification settings

MrLawrenceKwan/api-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

api-cache 🗄️

A tiny, flexible HTTP response caching middleware for Node.js. Supports in-memory, Redis, and custom backends.

Features

  • ⚡ In-memory LRU cache out of the box
  • 🔴 Optional Redis backend for distributed caching
  • 🎯 Route-level cache control with pattern matching
  • 🔑 Smart cache key generation (method + path + query + headers)
  • 📊 Cache hit/miss headers for debugging
  • 🧹 Automatic TTL-based expiration
  • 🪶 Express & Koa compatible

Quick Start

import express from 'express';
import { apiCache } from 'api-cache';

const app = express();

// Cache all GET requests for 60 seconds
app.use(apiCache({ ttl: 60 }));

// Route-specific caching
app.get('/api/users', apiCache({ ttl: 300 }), (req, res) => {
  res.json(users);
});

// Skip cache for authenticated requests
app.use(apiCache({
  ttl: 120,
  skip: (req) => !!req.headers.authorization,
}));

Redis Backend

import { apiCache, RedisStore } from 'api-cache';

app.use(apiCache({
  ttl: 300,
  store: new RedisStore({ url: 'redis://localhost:6379' }),
}));

License

MIT

About

Lightweight HTTP response caching middleware for Express

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published