{"library":"route-cache","title":"Route-Cache","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install route-cache"],"cli":null},"imports":["const routeCache = require('route-cache');","app.get('/path', routeCache.cacheSeconds(ttl), ...);","const IoRedisStore = require('route-cache/ioRedisStore');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const express = require('express');\nconst routeCache = require('route-cache');\nconst app = express();\n\n// Cache a route for 20 seconds using the default cache key (req.originalUrl)\napp.get('/cached-index', routeCache.cacheSeconds(20), (req, res) => {\n  console.log('This message will only appear once every 20 seconds for /cached-index');\n  res.send('This response is cached!');\n});\n\n// Cache a route with a custom, dynamic key based on user authentication status\napp.get('/user-data/:id', routeCache.cacheSeconds(60, (req, res) => {\n  // Assuming res.locals.isAuthenticated indicates user login status\n  if (!res.locals.isAuthenticated) return false; // Do not cache for unauthenticated requests\n  return req.originalUrl + '|' + req.params.id + '|' + res.locals.userId;\n}), (req, res) => {\n  res.locals.isAuthenticated = Math.random() > 0.5; // Simulate auth status\n  res.locals.userId = res.locals.isAuthenticated ? 'user123' : 'guest';\n  console.log(`Serving user data for ${req.params.id}. Authenticated: ${res.locals.isAuthenticated}`);\n  res.send(`Data for user ${req.params.id}. (Auth: ${res.locals.isAuthenticated})`);\n});\n\n// Example of how to explicitly remove a cached route\napp.post('/clear-cache', (req, res) => {\n  const routeToClear = req.body.route || '/cached-index';\n  routeCache.removeCache(routeToClear);\n  res.send(`Cache for ${routeToClear} cleared.`);\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`Server listening on port ${PORT}`);\n  console.log('Try visiting /cached-index multiple times, and /user-data/1 and /user-data/2');\n});","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}