apicache is an ultra-simplified middleware for Express.js and Node.js designed to cache API responses. It supports both an in-memory store and Redis as a backend, allowing developers to define cache durations using plain-english strings (e.g., '5 minutes'). The current stable version is 1.6.3, with version 1.0.0 marking a production-ready release after extensive use. The project appears to have a moderate release cadence, focusing on stability and adding features incrementally, such as `res.write` support and official `compression` integration. Its key differentiator is its ease of use and 'automagic' caching injection into routes, making it a straightforward choice for basic API caching needs compared to more complex caching solutions.
npm install apicacheVerified import paths — ran on the pinned version, not inferred.
This example sets up an Express server with `apicache` configured for either in-memory or Redis caching. It demonstrates how to apply caching middleware to a route, retrieve cache performance metrics, view the cache index, and manually clear cached entries by target or group.
Ensure `app.use(compression());` comes before `app.use(apicache.middleware('duration'));` or before any route-specific cache middleware.Adjust any custom middleware that relies on `apicache` headers to check for their presence during the response phase, specifically when a cache hit occurs.
For detailed logging, set the environment variable `export DEBUG=apicache` before running your Node.js application instead of relying solely on `options({ debug: true })`.Upgrade your Node.js environment to version 8 or newer.
Set global options before creating or using the `middleware` instance: `let cache = apicache.options({ /* ... */ }).middleware;`Ensure you are importing the default `apicache` object: `import apicache from 'apicache';` and then accessing `middleware` as a property: `let cache = apicache.middleware;`
Start your Redis server, verify its configuration (host/port), and ensure your application has network access to it. If using a custom URL, ensure `redis.createClient({ url: '...' })` is correct.Replace `const apicache = require('apicache');` with `import apicache from 'apicache';`Verify the target string passed to `apicache.clear(target)` matches an exact URL path or a `req.apicacheGroup` name. Use `apicache.getIndex()` to inspect current cached entries and groups to ensure correct targeting.