ExpressJS / KeystoneJS Caching

Posted on December 21st, 2014 in Node.js, Development, Caching by Rich

Recently it came to my attention that ExpressJS; although brilliant, didn't offer any application caching out of the box, so I decided to write some! Introducing ExpressJS Redis Cache.

This package (installable from npm) adds a middleware layer which caches incoming requests, keyed via the URL path, into a Redis store.

Installation is simple:

npm install expressjs-redis-cache

Usage

redisCache = require('expressjs-redis-cache')
...
//insert before app.router middleware
app.use(redisCache.createCache(options));
app.use(app.router)

Configuration

All of the options are optional and have appropriate defaults

  • client (default: null)
    • provide an already initialised redis client
  • redisConnString (default: localhost)
    • Connection String for the Redis Instance if client isn't provided
  • cacheDuration (default: 60*5)
    • How long Responses are cached for
  • cacheControl (default: "public")
    • Sets the Cache-Control HTTP Header
  • cacheExceptions (default: [])
    • Array of routes which should be excluded from the cache. E.g. ['/admin/*', '/private']
comments powered by Disqus