Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fabioricali/koa-incache
Koa cache middleware
https://github.com/fabioricali/koa-incache
Last synced: about 2 months ago
JSON representation
Koa cache middleware
- Host: GitHub
- URL: https://github.com/fabioricali/koa-incache
- Owner: fabioricali
- Created: 2017-08-20T18:46:02.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-13T01:55:32.000Z (almost 2 years ago)
- Last Synced: 2024-10-12T19:04:45.690Z (2 months ago)
- Language: JavaScript
- Size: 189 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
## Installation
Koa-incache requires **koa2** and **node v7.6.0 or higher**
```
npm install koa-incache --save
```# Example
#### Basic usage
```javascript
const cache = require('koa-incache');
const koa = require('koa');
const app = new koa();app.use(cache());
app.use(ctx=>{
const result = 'hello world';
ctx.cached(result);
ctx.body = result;
});app.listen(3000);
```#### Routing
The **cache is made** using `ctx.originalUrl` as key
```javascript
const fs = require('fs');
const cache = require('koa-incache');
const koa = require('koa');
const Router = require('koa-router');
const app = new koa();
const router = new Router();app.use(cache({
maxAge: 60000 // 1 minute max age
}));router.get('/this/is/cached', function (ctx, next) {
const content = fs.readFileSync('myFile');
ctx.cached(content);
ctx.body = content;
});router.get('/this/is/not/cached', function (ctx, next) {
const content = fs.readFileSync('myFile');
ctx.body = content;
});router.get('/uncache', function (ctx, next) {
const content = fs.readFileSync('myFile');
ctx.cached(content);
if(foo)
ctx.uncache();
ctx.body = content;
});app
.use(router.routes())
.use(router.allowedMethods());app.listen(3000);
```#### Access to InCache instance
From koa context it's possible get InCache by `ctx.cache````javascript
router.get('/my/root', function (ctx, next) {
ctx.cache.set('my key', 'my value');
//...
});
```## API context
- `cached` accept an argument that is the content to be stored
- `uncache` remove cache for context rootFor more info **about InCache** click here
## Changelog
You can view the changelog here## License
koa-incache is open-sourced software licensed under the MIT license## Authors
Fabio Ricali