Route-Cache

0.7.0 · active · verified Wed Apr 22

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

const express = require('express');
const routeCache = require('route-cache');
const app = express();

// Cache a route for 20 seconds using the default cache key (req.originalUrl)
app.get('/cached-index', routeCache.cacheSeconds(20), (req, res) => {
  console.log('This message will only appear once every 20 seconds for /cached-index');
  res.send('This response is cached!');
});

// Cache a route with a custom, dynamic key based on user authentication status
app.get('/user-data/:id', routeCache.cacheSeconds(60, (req, res) => {
  // Assuming res.locals.isAuthenticated indicates user login status
  if (!res.locals.isAuthenticated) return false; // Do not cache for unauthenticated requests
  return req.originalUrl + '|' + req.params.id + '|' + res.locals.userId;
}), (req, res) => {
  res.locals.isAuthenticated = Math.random() > 0.5; // Simulate auth status
  res.locals.userId = res.locals.isAuthenticated ? 'user123' : 'guest';
  console.log(`Serving user data for ${req.params.id}. Authenticated: ${res.locals.isAuthenticated}`);
  res.send(`Data for user ${req.params.id}. (Auth: ${res.locals.isAuthenticated})`);
});

// Example of how to explicitly remove a cached route
app.post('/clear-cache', (req, res) => {
  const routeToClear = req.body.route || '/cached-index';
  routeCache.removeCache(routeToClear);
  res.send(`Cache for ${routeToClear} cleared.`);
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}`);
  console.log('Try visiting /cached-index multiple times, and /user-data/1 and /user-data/2');
});

view raw JSON →