unusual-cache
v1.1.2
Published
Simple pluggable cache middleware for Express with TTL support
Readme
What is Unusual Cache?
Is a lightweight, pluggable caching middleware for express.js that brings fine-grained response caching with customizable TTL, key generation, and optional external storage backends.
Instalation
npm install unusual-cacheor using yarn:
yarn add unusual-cacheUsage
| Property | Type | Default | Description |
| -------------- | ------------------- | --------------------------- | ------------------------------------------- |
| ttl | number | 60 (seconds) | Time to live for cache entries |
| keyGenerator | (req) => string | (req) => req.originalUrl | Function to generate cache key from request|
| store | CacheStore | Map() | Custom cache store implementing .get() and .set() |
import express from 'express';
import cache from 'unusual-cache';
const app = express();
const slowFunction = async () => {
console.log('⏳ slowFunction called...');
await new Promise((resolve) => setTimeout(resolve, 3000));
return { message: '✅ Response from slowFunction' };
};
app.get('/api/data', cache({
ttl: (req) => req.query.noCache ? 0 : 30,
keyGenerator: (req) => req.originalUrl.split('?')[0],
}), async (req, res) => {
const data = await slowFunction();
res.json(data);
});
app.listen(3000, () => {
console.log('🚀 Server running on http://localhost:3000');
});
