HTTP response caching for Koa
HTTP response caching for Koa. Caches the response based on any arbitrary store you'd like.
appuserequire'koa-cash'// some optionsappuse// this response is already cashed if `true` is returned,// so this middleware will automatically serve this response from cacheif yield* thiscashed return// set the response body here,// and the upstream middleware will automatically cache itthisresponsebody = 'hello world!'
Default max age for the cache if not set via
Minimum byte size to compress response bodies. Default
A hashing function. By default, it's:
this is the Koa context and is also passed as an argument.
By default, it caches based on the URL.
Get a value from a store. Must return a "yieldable", which returns the cache's value, if any.
Note that all the
maxAge stuff must be handled by you.
This module makes no opinion about it.
Set a value to a store. Must return a "yieldable".
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 ageappuserequire'koa-cash'return cachegetkeycachesetkey value
This is how you enable a route to be cached.
If you don't call
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
then the current request has been served from cache and __you should early
Otherwise, continue setting
this.response.body= and this will cache the response.
HEADrequests are cached.
200responses are cached. Don't set
304status codes on these routes - this middleware will handle it for you
Dateobjects as well as
Bufferobjects. Otherwise, you may have to serialize/deserialize yourself.