http-cache-middleware is a high-performance connect-like HTTP cache middleware designed for Node.js applications. It leverages the popular `cache-manager` package, providing flexibility to integrate various storage engines like in-memory, Redis, and more. The library significantly reduces latency by enabling robust caching strategies, capable of improving response times from tens of milliseconds down to single digits. It supports custom `x-cache-timeout` and `x-cache-expire` headers for fine-grained control over cache entry and invalidation using glob patterns. Additionally, it transparently handles standard HTTP `Cache-Control` and `ETag` headers to facilitate browser-side caching and validation. The current stable version is 1.4.1, with recent updates indicating an active maintenance and development cadence focused on fixes and dependency updates, with minor cumulative releases.
npm install http-cache-middlewareVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the HTTP cache middleware with `restana`, set a cache timeout for a GET endpoint, and invalidate it using a DELETE request with `x-cache-expire`.
Use supported units for `x-cache-timeout` (e.g., '1s', '1m', '1h') or provide a numerical value in seconds directly if using an alternative method for timeout.
Be aware that the first request to a resource will not contain the generated browser cache headers. These headers appear only once the response is stored in the internal `http-cache-middleware` cache and subsequently served from it.
Ensure your `x-cache-expire` patterns are correctly defined according to `matcher` package specifications. Review any existing invalidation logic that might have worked around the previous bug.
No direct fix needed, but be aware of this change if you have observed or relied on specific timing in cache write operations under heavy load. This is generally a stability improvement.
Update to `v1.3.10` or newer to prevent errors when calling `cache.del` (implicitly via `x-cache-expire`) with patterns that resolve to an empty set of keys.
Ensure you are on v1.4.1 or newer for reliable conditional middleware behavior using `iff` or `unless`.
Upgrade `http-cache-middleware` to version 1.3.10 or newer. This version includes a fix that gracefully handles empty key arrays.
Update `http-cache-middleware` to version 1.3.8 or newer. Ensure your patterns are valid according to the `matcher` package syntax (e.g., `*/users` to match all paths ending with `/users`).
Ensure you invoke the factory function: `const middleware = require('http-cache-middleware')();`