koa-response-cache

1.0.5 • Public • Published

NPM version License

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-cache

Example

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: String or Function
    • 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
  • expire
    • type: Number
    • redis expire time (second), default is 30 * 60 (30 min)
  • passParam
    • type: String
    • if the passParam is existed in query string, not get from cache
  • maxLength
    • type: Number
    • max length of the body to cache
  • routes
    • type: Array
    • the routes to cache, default is ['(.*)']
    • it could be ['/api/(.*)', '/view/:id'], see path-to-regexp
  • exclude
    • type: Array
    • the routes to exclude, default is []
    • it could be ['/api/(.*)', '/view/:id'], see path-to-regexp
  • onError
    • type: Function
    • callback function for error, default is function() {}
  • condition
    • type: Function
    • should be function(ctx) {} and it should return true or false to match redis cache condition
  • isBackup
    • type: Boolean
    • if set to true, then cache will backup, default is false
  • expireDump
    • type: Number
    • backup redis expire time (second), default is 120 * 60 (2 hours)
  • checkDump
    • type: Function
    • should be function(ctx) {} and it should return true or false, if return true then will return backup cache
  • redis
    • type: Object
    • redis options
  • redis.port
    • type: Number
  • redis.host
    • type: String
  • redis.options

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-cache will set a custom http header X-Koa-Response-Cache: true when the response is from cache, and will set X-Koa-Response-Cache: false when the response is from backup cache.

License

MIT

Package Sidebar

Install

npm i koa-response-cache

Weekly Downloads

5

Version

1.0.5

License

MIT

Unpacked Size

10.1 kB

Total Files

3

Last publish

Collaborators

  • schwann