Awesome
s3-cache
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:
key
secret
bucket
Other options:
salt
- add a salt to namespace yourcache
instances
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
- 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
.