1.0.5 • Published 7 years ago
koa-response-cache v1.0.5
koa-response-cache
a middleware for koa to cache response with redis.
extend from koa-redis-cache
https://github.com/coderhaoxin/koa-redis-cache
Extra Features:
- support extra match condition. (eg. when mobile visit not get from cache)
- support cache backup, cache backup for high availability.
Installation
$ npm install koa-response-cacheExample
const cache = require('koa-response-cache');
const koa = require('koa');
const app = koa();
let options = {
expire: 60,
routes: ['/index']
}
app.use(cache(options));options
- prefix
- type:
StringorFunction - redis key prefix, default is
koa-response-cache: - if a function is supplied, its signature should be
function(ctx) {}and it should return a string to use as the redis key prefix
- type:
- expire
- type:
Number - redis expire time (second), default is
30 * 60(30 min)
- type:
- passParam
- type:
String - if the passParam is existed in query string, not get from cache
- type:
- maxLength
- type:
Number - max length of the body to cache
- type:
- routes
- type:
Array - the routes to cache, default is
['(.*)'] - it could be
['/api/(.*)', '/view/:id'], see path-to-regexp
- type:
- exclude
- type:
Array - the routes to exclude, default is
[] - it could be
['/api/(.*)', '/view/:id'], see path-to-regexp
- type:
- onError
- type:
Function - callback function for error, default is
function() {}
- type:
- condition
- type:
Function - should be
function(ctx) {}and it should returntrueorfalseto match redis cache condition
- type:
- isBackup
- type:
Boolean - if set to
true, then cache will backup, default isfalse
- type:
- expireDump
- type:
Number - backup redis expire time (second), default is
120 * 60(2 hours)
- type:
- checkDump
- type:
Function - should be
function(ctx) {}and it should returntrueorfalse, if returntruethen will return backup cache
- type:
- redis
- type:
Object - redis options
- type:
- redis.port
- type:
Number
- type:
- redis.host
- type:
String
- type:
- redis.options
- type:
Object - see node_redis
- type:
set different expire for each route
const cache = require('koa-response-cache');
const koa = require('koa');
const app = koa();
let options = {
routes: [{
path: '/index',
expire: 60
}, {
path: '/user',
expire: 5
}]
}
app.use(cache(options));notes
koa-response-cachewill set a custom http headerX-Koa-Response-Cache: truewhen the response is from cache, and will setX-Koa-Response-Cache: falsewhen the response is from backup cache.
License
MIT