route-cache is an Express.js middleware designed for efficient server-side caching of HTTP route responses. Its primary goal is to accelerate backend performance for frequently accessed routes, alleviate server load during periods of high demand, and mitigate issues like the 'thundering herd' phenomenon by serving pre-computed content for a defined period (TTL). The package is currently at version 0.7.0. Release cadence appears sporadic, with the last notable update being 0.6.1, which included status code caching. Key advantages of `route-cache` include broad support for various content types, proper handling of HTTP redirects, the ability to implement conditional caching logic per request, including dynamic cache key generation based on `req` and `res` objects, and seamless integration with gzip compression. It provides both an in-memory caching solution (leveraging `lru-cache` internally) and extensibility for distributed caching mechanisms through pluggable stores, such as `IoRedisStore` for Redis integration. This flexibility makes it suitable for both small-scale applications and larger, distributed systems requiring shared cache states.
npm install route-cacheVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `route-cache` with an Express application, showing basic route caching with a TTL, dynamic cache key generation based on request/response properties (including conditional caching), and manual cache invalidation.
Upgrade to v0.1.2 or newer. If you need to cache specific error responses, implement custom caching logic that explicitly overrides this behavior via a dynamic key function.
Review routes where status code caching might introduce unexpected behavior. If dynamic status codes are critical, consider conditional caching or dynamic keys that factor in status logic.
Always define a custom cache key function (`routeCache.cacheSeconds(ttl, function(req, res){ ... })`) for routes serving personalized or highly dynamic content to ensure proper cache isolation. Return `false` from the function to prevent caching for specific requests.Stick to CommonJS `require()` for importing `route-cache` and its sub-modules to ensure compatibility, or verify ESM support in your specific Node.js version and project configuration.
Upgrade `route-cache` to version 0.4.7 or higher to resolve the underlying JSON null error.
Either decrease the `ttl` (Time-To-Live) for `cacheSeconds`, or explicitly call `routeCache.removeCache('/your-route')` after data updates to invalidate the specific cached entry. For production, consider using distributed caching with invalidation hooks.Implement a custom cache key function for the affected route. The key should incorporate identifying information like `req.session.userId` or a unique user identifier, e.g., `routeCache.cacheSeconds(ttl, (req, res) => req.originalUrl + '|' + res.locals.userId);`.