# koa-s3-cache

> Koa middleware to cache and serve from S3

Latest version **1.1.0** (published 2015-04-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-s3-cache
pnpm add koa-s3-cache
yarn add koa-s3-cache
bun add koa-s3-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.0 |
| Published | 2015-04-05 |
| First published | 2015-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 13 |
| Author | Jonathan Ong |
| Maintainers | jongleberry |
| Keywords | koa, s3, cache |

## Links

- npm: https://www.npmjs.com/package/koa-s3-cache
- Repository: https://github.com/koajs/s3-cache
- Issues: https://github.com/koajs/s3-cache/issues
- npm.io page: https://npm.io/package/koa-s3-cache

## Dependencies (4)

- [knox](https://npm.io/package/knox.md) 0
- [fs-cp](https://npm.io/package/fs-cp.md) ^1.3.0
- [temp-path](https://npm.io/package/temp-path.md) ^1.0.0
- [on-finished](https://npm.io/package/on-finished.md) ^2.2.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

- 1.1.0 (latest) — 2015-04-05
- 1.0.0 — 2015-04-05
- 0.0.0 — 2015-04-05

## README

# s3-cache

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]
[![Gittip][gittip-image]][gittip-url]

Caches and serves responses to and from S3.
Similar to [koa-cash](https://github.com/koajs/cash),
except it optimizes for the S3 use-case by streaming.

Usage:

```js
let cache = require('koa-s3-cache')({
  // s3 credentials and stuff
})

app.use(function* (next) {
  // served from cache
  if (yield* cache.get(this)) return

  // do some crazy computation
  this.body = new Buffer(1024 * 1024)

  // save it to s3
  yield* cache.put(this)
})
```

This is best for dynamically created content that is cached (i.e. thumbnails).
Instead of caching yourself in the business logic,
cache transparently with this module.

## API

### const cache = Cache(options)

Create a `cache` instance.

S3 options:

- `key`
- `secret`
- `bucket`

Other options:

- `salt` - add a salt to namespace your `cache` instances

### app.use(cache)

You can use the cache as middleware,
which caches all downstream middleware.

```js
app.use(cache)

app.use(function* () {
  this.body = 'something computationally intensive'
})
```

### app.use(cache.wrap( next => ))

Wrap a middleware with the cache.
Useful for conditional caching

```js
app.use(cache.wrap(function* () {
  this.body = 'something computationally intensive'
}))
```

### const served = yield cache.get(this)

Serve this request from the cache.
Returns `served`, which is whether the response has been served from the cache.

### yield cache.put(this)

Caches the current response.

## Notes

- You should set an object lifecycle rule.
  Ex. delete all files in the bucket after 7 days.
- Objects are stored with `REDUCED_REDUNDANCY`.
- Only supports `200-2` status codes.
- If the body is streaming, the stream is cached to the filesystem so that the S3 client knows its `content-length`.

[npm-image]: https://img.shields.io/npm/v/koa-s3-cache.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-s3-cache
[github-tag]: http://img.shields.io/github/tag/koajs/s3-cache.svg?style=flat-square
[github-url]: https://github.com/koajs/s3-cache/tags
[travis-image]: https://img.shields.io/travis/koajs/s3-cache.svg?style=flat-square
[travis-url]: https://travis-ci.org/koajs/s3-cache
[coveralls-image]: https://img.shields.io/coveralls/koajs/s3-cache.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/koajs/s3-cache
[david-image]: http://img.shields.io/david/koajs/s3-cache.svg?style=flat-square
[david-url]: https://david-dm.org/koajs/s3-cache
[license-image]: http://img.shields.io/npm/l/koa-s3-cache.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/koa-s3-cache.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/koa-s3-cache
[gittip-image]: https://img.shields.io/gratipay/jonathanong.svg?style=flat-square
[gittip-url]: https://gratipay.com/jonathanong/

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