Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nswbmw/koa-router-cache
Router cache middleware for koa.
https://github.com/nswbmw/koa-router-cache
Last synced: 2 months ago
JSON representation
Router cache middleware for koa.
- Host: GitHub
- URL: https://github.com/nswbmw/koa-router-cache
- Owner: nswbmw
- Created: 2015-03-19T11:18:51.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-01-03T14:28:21.000Z (about 8 years ago)
- Last Synced: 2024-10-04T22:05:20.329Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 22
- Watchers: 4
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-github-star - koa-router-cache
- awesome-koa - koa-router-cache - 路由缓存中间件。![](https://img.shields.io/github/stars/nswbmw/koa-router-cache.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-router-cache.svg?style=flat-square) (仓库 / 中间件)
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