JSPM

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

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.

Release notes

koa-router-cache v1.0.0 released, v0.3.0 readme see v0.3.0_README.md.

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 Boolean. (Required)
  • 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('../');
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: function* (_cache) {
      if (_cache == null) {
        return true;
      }
      this.body = _cache;
      return false;
    },
    evtName: 'clearIndexCache',
    destroy: MemoryCache.destroy
  }
}));

app.use(function* () {
  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('../');
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: function* (_cache) {
      if (_cache == null) {
        return true;
      }
      this.body = _cache;
      return false;
    },
    evtName: 'clearIndexCache',
    destroy: RedisCache.destroy
  }
}));

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

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

Test

npm test

License

MIT