Giter Site home page Giter Site logo

koajs / cash Goto Github PK

View Code? Open in Web Editor NEW
153.0 8.0 21.0 901 KB

HTTP response caching for Koa. Supports Redis, in-memory store, and more!

License: MIT License

JavaScript 100.00%
cache caching koa node javascript redis memory store storage s3 cdn aws amazon content delivery network session sessions static space

cash's Introduction

koa-cash

build status code coverage code style styled with prettier made with lass license npm downloads

HTTP response caching for Koa. Supports Redis, in-memory store, and more!

Table of Contents

Features

Caches the response based on any arbitrary store you'd like.

  • Handles JSON and stream bodies
  • Handles gzip compression negotiation (if options.compression is set to true as of v4.0.0)
  • Handles 304 responses

๐ŸŽ‰ Pairs great with @ladjs/koa-cache-responses ๐ŸŽ‰

Install

NPM

npm install koa-cash

Yarn

yarn add koa-cash

Usage

import LRU from 'lru-cache';
import koaCash from 'koa-cash';

// ...
const cache = new LRU();
app.use(koaCash({
  get: (key) => {
    return cache.get(key);
  },
  set(key, value) {
    return cache.set(key, value);
  },
}))

app.use(async ctx => {
  // this response is already cashed if `true` is returned,
  // so this middleware will automatically serve this response from cache
  if (await ctx.cashed()) return;

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

API

app.use(koaCash(options))

Options are:

maxAge

Default max age (in milliseconds) for the cache if not set via await ctx.cashed(maxAge).

threshold

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

compression

If a truthy value is passed, then compression will be enabled. This value is false by default.

setCachedHeader

If a truthy value is passed, then X-Cached-Response header will be set as HIT when response is served from the cache. This value is false by default.

methods

If an object is passed, then add extra HTTP method caching. This value is empty by default. But GET and HEAD are enabled.

Eg: { POST: true }

hash()

A hashing function. By default, it's:

function hash(ctx) {
 return ctx.response.url; // same as ctx.url
}

ctx 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 Promise, which returns the cache's value, if any.

function get(key, maxAge) {
  return Promise;
}

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

function set(key, value, maxAge) {
  return Promise;
}

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.

const koaCash = require('koa-cash');
const LRU = require('lru-cache');

const cache = new LRU({
  maxAge: 30000 // global max age
})

app.use(koaCash({
  get (key, maxAge) {
    return cache.get(key)
  },
  set (key, value) {
    cache.set(key, value)
  }
}))

See @ladjs/koa-cache-responses test folder more examples (e.g. Redis with ioredis).

Max age (optional)

const cached = await ctx.cashed(maxAge) // maxAge is passed to your caching strategy

This is how you enable a route to be cached. If you don't call await ctx.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 ctx.body= and this will cache the response.

CashClear

ctx.cashClear('/')

This is a special method available on the ctx that you can use to clear the cache for a specific key.

Notes

  • Only GET and HEAD requests are cached. (Unless overridden)
  • 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.

Contributors

Name Website
Jonathan Ong http://jongleberry.com
Nick Baugh http://niftylettuce.com

License

MIT ยฉ Jonathan Ong

Links

cash's People

Contributors

amitport avatar dependabot[bot] avatar edorivai avatar eknkc avatar greenkeeperio-bot avatar jonathanong avatar lbeschastny avatar nickmccurdy avatar niftylettuce avatar rek avatar shaunwarman avatar titanism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cash's Issues

Supporting request method POST

Any chance to support POST or the ability to configure supported methods with options?

I could do a PR, if you add me as a contributor.

readme.usage needs updating

copypasta readme.usage doesnt work

options object has couple required props

cash/index.js

Line 34 in cafda56

if (!get) throw new Error('.get not defined');

// error

Error: .get not defined
at module.exports (.../node_modules/koa-cash/index.js:34:19)

Support Koa 2

This library doesn't have a @next tag? Only generators?

Also would be nice if the examples were a bit more clear. I can't still manage to get it working and documentation is not so friendly.

I think this works now with lru-cache because their API is like:

set(key, value, maxAge)
get(key) => value

Both of these will update the "recently used"-ness of the key. They do what you think. maxAge is optional and overrides the cache maxAge option if provided.

Cancel caching

I'm in a situation where, in general, I want a route to be cached. But sometimes I figure out on the way back through the middleware stack that I don't want it cached for this specific request.

To be a bit more precise: we have a situation where we need to poll for results, which can take up to 2 minutes. After the polling has completed, we cache those results for, say, 15 minutes. During this period after polling completion, we want any requests to this specific route to be cached.

A schematic solution could look like this:

app.use(cash( { ... } ));

app.use(async (ctx, next) => {
    // In general I want all traffic beyond this middleware to be cached
    if (await ctx.cached()) return;
    return next();
});

app.use(async ctx => {
    // fancyRenderLogic could determine that a specific response should not be cached
    await fancyRenderLogic(ctx);
    if (ctx.shouldNotCache) { // could be set by fancyRenderLogic
        ctx.uncached(); // Or something like this where one could cancel
    }
});

Please let me know what you think

Cache headers

On my API I have a X-Total-Count header. How can I cache it? Is it interesting to have this feature on this library? If you agree it's a good idea, I can implement it and send a PR.

Binary response support (application/octet-stream)

Hi there! It seems that cash doesn't support binary responses (like application/octet-stream).

Eg. I'm trying to response with pbf like

ctx.body = Buffer.from(geobuf.encode(geojson, new Pbf()));

And getting

Error: Place koa-cache below any compression middleware.;

My response headers are:

HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: application/octet-stream
Content-Length: 2227
Content-Encoding: identity
ETag: "8b3-CASiLhVb9Ldvy7npP1sNNQ"
Date: Fri, 09 Dec 2016 14:40:13 GMT
Connection: keep-alive

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.