# koa-incache

> Koa cache middleware

Latest version **0.6.0** (published 2019-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-incache
pnpm add koa-incache
yarn add koa-incache
bun add koa-incache
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.0 |
| Published | 2019-08-01 |
| First published | 2017-08-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 80.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Fabio Ricali |
| Maintainers | fabioricali |
| Keywords | koa, cache, incache, in-memory, redis |

## Links

- npm: https://www.npmjs.com/package/koa-incache
- Repository: https://github.com/fabioricali/koa-incache
- Homepage: https://github.com/fabioricali/koa-incache#readme
- Issues: https://github.com/fabioricali/koa-incache/issues
- npm.io page: https://npm.io/package/koa-incache

## Dependencies (4)

- [flak](https://npm.io/package/flak.md) ^1.0.0
- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [defaulty](https://npm.io/package/defaulty.md) ^1.2.1
- [delete-key](https://npm.io/package/delete-key.md) ^1.1.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.6.0 (latest) — 2019-08-01
- 0.5.0 — 2019-01-07
- 0.4.0 — 2019-01-02
- 0.3.2 — 2018-12-20
- 0.3.1 — 2018-10-15
- 0.3.0 — 2017-09-07
- 0.2.0 — 2017-08-22
- 0.1.3 — 2017-08-21
- 0.1.2 — 2017-08-21
- 0.1.1 — 2017-08-20
- 0.1.0 — 2017-08-20

## README

<div align="center">
<h1>koa-incache</h1>
Koa cache middleware
<br/><br/>
<a href="https://travis-ci.org/fabioricali/koa-incache" target="_blank"><img src="https://travis-ci.org/fabioricali/koa-incache.svg?branch=master" title="Build Status"/></a>
<a href="https://coveralls.io/github/fabioricali/koa-incache?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/fabioricali/koa-incache/badge.svg?branch=master" title="Coverage Status"/></a>
<a href="https://opensource.org/licenses/MIT" target="_blank"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" title="License: MIT"/></a>
<img src="https://img.shields.io/badge/team-terrons-orange.svg" title="Team Terrons"/>
</div>

## 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 root

For more info **about InCache** <a target="_blank" href="https://github.com/fabioricali/incache">click here</a>

## Changelog
You can view the changelog <a target="_blank" href="https://github.com/fabioricali/koa-incache/blob/master/CHANGELOG.md">here</a>

## License
koa-incache is open-sourced software licensed under the <a target="_blank" href="http://opensource.org/licenses/MIT">MIT license</a>

## Authors
<a target="_blank" href="http://rica.li">Fabio Ricali</a>

<a target="_blank" href="https://www.mdslab.org">Davide Polano</a>

---
_Source: https://npm.io/package/koa-incache · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
