# koa2-url-cache

> Koa2 middleware for cache response data

Latest version **1.1.1** (published 2019-07-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa2-url-cache
pnpm add koa2-url-cache
yarn add koa2-url-cache
bun add koa2-url-cache
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2019-07-02 |
| First published | 2017-09-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node |  >7.0.0 |
| Dependencies | 4 |
| Unpacked size | 5.8 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | kissliux |
| Maintainers | kissliux |

## Links

- npm: https://www.npmjs.com/package/koa2-url-cache
- Repository: https://github.com/shanelau/koa-url-cache
- Issues: https://github.com/shanelau/koa-url-cache/issues
- npm.io page: https://npm.io/package/koa2-url-cache

## Dependencies (4)

- [redis](https://npm.io/package/redis.md) 2.8.0
- [winston](https://npm.io/package/winston.md) 2.4.4
- [bluebird](https://npm.io/package/bluebird.md) 3.5.3
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) 2.4.0

## Recent versions

- 1.1.1 (latest) — 2019-07-02
- 1.0.5 — 2017-09-20
- 1.0.4 — 2017-09-20
- 1.0.3 — 2017-09-20
- 1.0.2 — 2017-09-20
- 1.0.1 — 2017-09-20
- 1.0.0 — 2017-09-20

## README

# koa2-url-cache
Koa 2 middleware for cache response data. 

1. regular match url. [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
2. cache data in redis.

## Install

```
npm install koa2-url-cache --save
```
Get start

```
const config = {
  redis: {
    host: '121.196.203.34',
    port: 6379,
    password: '',
    db: 0,
  },
  urls: {
    '/api/user/:name': {
      ttl: 30, // seconds
    }
  }
}
app.use(cache(config));
```

## refresh
Delete cache manual, qury url add parameter `refresh=1`
### demo
`/api/user/list?refresh=1`

## Config

###  redis
[node_redis](https://github.com/NodeRedis/node_redis)

### prefix [optional]
1. Default 'page'
2. `prefix` is redis prefix key to storage cache data.

### disable [optional]
1. Default false
1. Diasble cache for all routes

###   loggerLevel [optional]
1. Default 'info'
2. Power by [winston](https://github.com/winstonjs/winston)

### http code 200
only cache http code==200 error

### addHeaders {Object} [optional]
Add all key-value to response headers.
```
  addHeaders: {
    "Access-Control-Allow-Origin": "http://localhost:8080",
    "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
    "Access-Control-Allow-Headers": "*"
  }
```
### urls {Object}
* `key` is match by [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
    * `ttl` {integer}  expire in ttl. seconds。no cache ,set `ttl = null`
    * `addHeaders` {Object}. It will cover the top level `addHeaders`

```
    '/api/user': {
      ttl: '30', // seconds
      addHeaders: { // custom headers
        'x-power':'lx'
      }
    },
```
example:
1. /foo/:bar
1. /:foo/:bar?
1. /:foo(\\d+)
more: [path-to-regexp](https://github.com/pillarjs/path-to-regexp)
...

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