Code Self Study Forum

How to Configure Redis for Development?

So I have installed Redis following this guide from digital ocean:

I have it running on local server to mirror what will happen in development, however, now the problem is that all pages get cached, and changes I make in development mode also get cached.

Is there a good work around for this? Should I define Development and Production variables in my build script, and then use boolean logic to turn redis off and on with that? The problem is that redis is implemented in all my routes… So I am not sure how that will work.

Here is how I call Redis in Express:

var cache = require('express-redis-cache')({
    auth_pass: process.env.PREDISP

cache.on('error', function (error) {
    throw new Error('Cache error!');

And then to cache a route:

router.get("/", cache.route(), ctrl.homePage);

Thanks for any help on how to turn redis off for development.

So here is the workaround thanks to Josh! :slight_smile:

If you are using Express Redis Cache you can set the cache time to be one second for development and 1 week for production. Here is the code.

Redis express cache:


global.cacheExpire = {
    200: process.env.NODE_ENV === "production"?604800:1,
    400: 1,
    403: 5,
    500: 1,

Add it to the cache route as follows:

cache.route({ expire: cacheExpire  }),
1 Like

Actually for some reason with the JSON I kept on getting an error message that there was no default cache value. So right now I am just setting the expiration for all response codes…