@danroshko/koa-cache

HTTP caching middleware for koa

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
31Jul 19, 2021Jul 20, 2018Minified + gzip package size for @danroshko/koa-cache in KB

Readme

Koa HTTP cache

Build Status npm version

HTTP caching middleware for koa

  • uses redis for storage
  • no need to manually add or remove entries. GET requests are automatically cached and POST, PUT, PATCH, DELETE requests automatically invalidate associated cache key
  • uses MD5 hash of the response and standard HTTP headers ETag and If-None-Match
  • works only with JSON payloads

Installation

npm i @danroshko/koa-cache

Usage

routes.js

const Router = require('koa-router');
const cache = require('koa-cache');

const caching = cache.createMiddleware(ctx => {
  // should return unique and stable cache key
  return ctx.state.user.id;
});

const router = new Router();
router.use(caching);

router.get('/', ctx => {
  // responses from this route will be cached
  ctx.body = { n: Math.random() };
});

router.post('/', ctx => {
  // requests to this route will invalidate cache
  ctx.status = 201;
});

module.exports = router;

app.js

const Koa = require('koa');
const Redis = require('ioredis');
const cache = require('koa-cache');
const router = require('./routes');

const koa = new Koa();
const redis = new Redis();

cache.configure({ redis, expires: 24 * 60 * 60 });

koa.use(router.routes());
koa.listen(3000);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.