Giter Site home page Giter Site logo

cash's Introduction

Koa Cash

NPM version Build status Test coverage Dependency Status License Downloads

HTTP response caching for Koa. Caches the response based on any arbitrary store you'd like.

  • Handles JSON and stream bodies
  • Handles gzip compression negotiation
  • Handles 304 responses
app.use(require('koa-cash')({
  // some options
}))

app.use(function* (next) {
  // this response is already cashed if `true` is returned,
  // so this middleware will automatically serve this response from cache
  if (yield this.cashed()) return

  // set the response body here,
  // and the upstream middleware will automatically cache it
  this.response.body = 'hello world!'
})

API

app.use(require('koa-cash')(options))

Options are:

maxAge

Default max age for the cache if not set via yield this.cashed(maxAge).

threshold

Minimum byte size to compress response bodies. Default 1kb.

hash()

A hashing function. By default, it's:

function hash(_this) {
  return this.request.url
}

this is the Koa context and is also passed as an argument. By default, it caches based on the URL.

get()

Get a value from a store. Must return a "yieldable", which returns the cache's value, if any.

function get(key, maxAge) {
  return <yieldable>
}

Note that all the maxAge stuff must be handled by you. This module makes no opinion about it.

set()

Set a value to a store. Must return a "yieldable".

function set(key, value, maxAge) {
  return <yieldable>
}

Note: maxAge is set by .cash={ maxAge }. If it's not set, then maxAge will be 0, which you should then ignore.

Example

Using a library like lru-cache, though this would not quite work since it doesn't allow per-key expiration times.

var cache = require('lru-cache')({
  maxAge: 30000 // global max age
})

app.use(require('koa-cash')({
  get (key, maxAge) {
    return cache.get(key)
  },
  set (key, value) {
    cache.set(key, value)
  }
}))

var cached = yield this.cashed([maxAge])

This is how you enable a route to be cached. If you don't call yield this.cashed(), then this route will not be cached nor will it attempt to serve the request from the cache.

maxAge is the max age passed to get().

If cached is true, then the current request has been served from cache and you should early return. Otherwise, continue setting this.response.body= and this will cache the response.

Notes

  • Only GET and HEAD requests are cached.
  • Only 200 responses are cached. Don't set 304 status codes on these routes - this middleware will handle it for you
  • The underlying store should be able to handle Date objects as well as Buffer objects. Otherwise, you may have to serialize/deserialize yourself.

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.