Hooking Cachegoose to Redis on NodeJS

Photo by jesse orrico on Unsplash

On my last post I outlined the steps required to enable caching using the Cachegoose NodeJS module. I thought it would be a fun exercise to add a Redis caching to the node-billz application. Previously I had implemented an in-memory cache which works fine for just me, but in an actual production environment memory could become an issue.

Cachegoose makes it very easy to add the Redis storage to the application since it uses the NodeJS cacheman plugin under the hood. Slight modifications to my main app.js file were required to pass the host, port and password to Redis. I also added the USE_REDIS flag to my .env file so Redis support can easily be toggled on or off without any code changes. Redis integration complete.

Another plus with using Redis is being able to explicitly set the cache eviction policy. The cacheman-memory module follows LRU (least recently used) eviction policy. Redis allows multiple eviction policies so it allows some flexibility.

Once deployed to the production environment I needed to be able to verify the cache was working correctly. I love using the cli but some things are just easier to visualize, uh, visually. Doing some quick searches I found a few open-source projects out there that were pretty nice, but Redsmin really stood out from the rest. I am using their free plan which allows monitoring of one Redis instance which is all I need.

Going forward I may revert back to the in-memory cache but it was a fun exercise adding Redis support with a few lines of code.

Like what you read? Give Robert Zehnder a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.