A tiny, flexible HTTP response caching middleware for Node.js. Supports in-memory, Redis, and custom backends.
- ⚡ 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
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,
}));import { apiCache, RedisStore } from 'api-cache';
app.use(apiCache({
ttl: 300,
store: new RedisStore({ url: 'redis://localhost:6379' }),
}));MIT