Giter Club home page Giter Club logo

s3-cache's Introduction

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:

  • 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.

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.

s3-cache's People

Contributors

jonathanong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

isabella232

s3-cache's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.