Hooking Cachegoose to Redis on NodeJS

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


Caching with NodeJS/Express

Working with payees and payments in node-billz I basically end up loading and reloading the same data on every page. The list of people paid each month will rarely change. The list of payments will grow over time but generally only a few payments are made per week. Basically, these are perfect candidates for caching so I thought I would see how difficult it would be to get this implemented in Node.

Searching GitHub I quickly found a few cache providers that would work with MongoDB and more specifically Mongoose since that is what I am using to work with backend data. There were a few false starts with downloading orphaned projects that were no longer supported, but finally I settled with cachegoose. It has been updated recently plus it gave me the option of an in-memory cache as well as caching with Redis and others.

The cache provider module selected, the next step was to get the app configured to use the cache. Two lines of code later everything was working. It does not get much easier. I opted to use the in-memory cache for the time being, but you can easily point cachegoose to a Redis server at initialization.

The final step was to wire the cache to the queries. You can specify the number of seconds you would like to cache a query or you can cache it indefinitely with a custom cache key. If a cache needs to be purged, call the clearCache method with the custom cache key to clear it. I opted to go the second route since the data only changes occasionally. When a payee is modified or a payment is made I clear the cache keys so the data can be re-cached the next time it is queried.

It was incredibly easy to setup caching, aside from the false steps I had at first. Given the size of the ecosystem though I suppose I should not be surprised to find some dead projects out there.

I would be interested in learning about any other libraries or tools you guys are using. Feel free to comment or drop me a line.

Welcome to 2018

2017 was a very quiet year for me as far as blogging has been concerned and that is something I intend to remedy in 2018. I thought I would go ahead and kick things off since there is no time like the present.

While things have been quiet I was actually quite busy in 2017. The first part of the year was spent picking up Angular(JS/2/4/5). That kept me occupied for the early part of 2017. Next I wanted to look in to ReactJS because I have heard so many good things about the framework. Turns out it is not the easiest framework to pickup so I decided to put it down for a bit.

After working with frontend frameworks for the first half of the year I thought it would be great to move javascript to the backend and decided to pick up NodeJS and ExpressJS. I will not really expand on this too much but overall I have really enjoyed my time tinkering around (have you checked out node-billz, my NodeJS-based bill calendar)? Be on the lookout for more posts coming soon.

As far as non-tech things, I did not read quite as many books in 2017 as I had intended. I got started on the Wheel of Time series by Robert Jordan and I made it to book seven, I hope to finish the series this year. Also Raymond E. Feist, arguably my favorite author of all time, has a new series coming out in March.

2018 is going to be a great year!