Home

Awesome

s3-cache

NPM version Build status Test coverage Dependency Status License Downloads

Caches and serves responses to and from S3. Similar to koa-cash, except it optimizes for the S3 use-case by streaming.

Usage:

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:

Other options:

app.use(cache)

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

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

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