JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 10
  • Score
    100M100P100Q44926F

Router cache middleware for koa.

Package Exports

  • koa-router-cache

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-router-cache) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

koa-router-cache

Router cache middleware for koa@1.

Install

npm i koa-router-cache --save

Usage

cache(app, options)

Options:

  • key: {String|GeneratorFunction} cache key. (Required)
  • expire: {Number} expire time, in ms. (Required)
  • get: {GeneratorFunction} custom getter function for getting data from cache. (Required)
  • set: {GeneratorFunction} custom setter function for setting data to cache. (Required)
  • passthrough: {GeneratorFunction} whether pass request through, return an object. (Required)
    • shouldCache: {Boolean} whether cache this result
    • shouldPass: {Boolean} whether pass this request through
  • evtName: {String} event name for destroy cache. (Optional)
  • destroy: {function} destroy cache. (Optional)
  • pathToRegexp {Object} pathToRegexp options, see https://github.com/pillarjs/path-to-regexp#usage. (Optional)

Example

koa-router-cache build-in simple MemoryCache and RedisCache, also you can write by yourself.

MemoryCache

'use strict';
 
var app = require('koa')();
var cache = require('koa-router-cache');
var MemoryCache = cache.MemoryCache;
 
var count = 0;
 
app.use(cache(app, {
  'GET /': {
    key: 'cache:index',
    expire: 2 * 1000,
    get: MemoryCache.get,
    set: MemoryCache.set,
    passthrough: MemoryCache.passthrough,
    evtName: 'clearIndexCache',
    destroy: MemoryCache.destroy
  }
}));
 
app.use(function* () {
  if (this.path === '/') {
    this.body = count++;
    if (count === 3) {
      count = 0;
      this.app.emit('clearIndexCache');
    }  
  }
});
 
app.listen(3000, function () {
  console.log('listening on 3000.');
});

RedisCache

'use strict';

var app = require('koa')();
var cache = require('koa-router-cache');
var RedisCache = cache.RedisCache();// or RedisCache(client)

var count = 0;

app.use(cache(app, {
  'GET /': {
    key: 'cache:index',
    expire: 2 * 1000,
    get: RedisCache.get,
    set: RedisCache.set,
    passthrough: RedisCache.passthrough,
    evtName: 'clearIndexCache',
    destroy: RedisCache.destroy
  }
}));

app.use(function* () {
  if (this.path === '/') {
    this.body = {
      count: count++
    };
    if (count === 3) {
      count = 0;
      this.app.emit('clearIndexCache');
    }
  }
});

app.listen(3000, function () {
  console.log('listening on 3000.');
});

another usage:

// Guests share one index page cache
GET /': {
  key: 'cache:index',
  expire: 10 * 1000,
  get: MemoryCache.get,
  set: MemoryCache.set,
  destroy: MemoryCache.destroy,
  passthrough: function* passthrough(_cache) {
    // Guests
    if (!this.session || !this.session.user) {
      if (_cache == null) {
        return {
          shouldCache: true,
          shouldPass: true
        };
      }
      this.type = 'text/html; charset=utf-8';
      this.set('content-encoding', 'gzip');
      this.body = _cache;
      return {
        shouldCache: true,
        shouldPass: false
      };
    }
    // Logged-in users
    return {
      shouldCache: false,
      shouldPass: true
    };
  }
}

Test

npm test

License

MIT