HTTP caching middleware for koa


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


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


npm i @danroshko/koa-cache



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


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 });


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.