ITP Express Redis Cache
A light api route cache system with Redis for Express.js
Installation
npm install itp-express-redis-cache
Usage
const express = ;const app = ; const ITPExpressRedisCache = ; app; app;
Options
Init options:
const ITPExpressRedisCache = port: 6379 // redis port host: 'localhost' // redis host authPass: null // redis pass db: 0 // redis database id prefix: 'my-sample-app' // redis key prefix, e.g. 'my-sample-app:route:GET:/' enabled: true // disable/enable route caching, for example in debug mode excludeStatuscodes: 500 // disable response caching based on response statuscode. Possible values: number, array, function (excludes 500 and higher by default);
The excludeStatuscodes parameter can also be an array:
const ITPExpressRedisCache = port: 6379 host: 'localhost' authPass: null db: 0 prefix: 'my-sample-app' enabled: true excludeStatuscodes: 404 406 408 410 // disable response caching based on response statuscode. Possible values: number, array, function (excludes 500 and higher by default);
Route middleware options:
ITPExpressRedisCache
The route key parameter can also be a function:
ITPExpressRedisCache
The expire parameter can also be a function:
ITPExpressRedisCache
Application-level middleware
Simply use app.use
of express to use ITPExpressRedisCache
as an Application-level middleware.
app
Disable Caching inside the Route
You can disable caching for specific routes by adding res.skipCache = true
to opt out the route from getting cached.
app;
Supported env variables
- REDIS_HOST
- REDIS_PORT
- REDIS_PASS
- REDIS_DB
License
ITP-Express-Redis-Cache is freely distributable under the terms of the MIT license.