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.

Advertisements

My Node/Express/Angular adventures


Lately I have focused a lot of my attention on Node.js, Express and Angular. One of the great things about Node is how extremely flexible the language is. However, when you are learning a new technology that flexibility can be a liability.

My first attempt at learning Node.js and Express was focused implementing framework paradigms that I would use developing a ColdFusion application. My application was a Diablo 3 character differ that would allow you to compare two characters side by side. It was a good lesson in how to structure a pure Express application. Plus, it worked, which was awesome.

Next I decided to focus on Angular and since I was trying to run it on a MEAN stack of course the first two projects I checked out were MEAN.IO and Mean.js. Both of these are really great projects, but when you are just trying to pick the stack up I was just completely lost.

When that did not pan out, I tried getting started with Node and Express using the same framework I used with the character differ. I was using Handlebars for server side render engine on Express which unfortunately conflicts with variables in Angular. Yes, I know you can change the brackets with $interpolateProvider, but I did not want to muck about changing things when I shouldn’t have to.

I was a little annoyed, but I was just going to develop the Angular portion of the app served with Nginx and write the API portion in Node/Express. I was taking a look at John Papa’s Angular style guide though and many of the configuration blocks just falling into place. Following John’s style guide along with his ng-demos repo I had my new Node/Express/Angular app up and running in no time.

If you have not checked those out, I highly recommend it. I might head back to MEAN.IO or Mean.js eventually, but I am enjoying learning the new stack for now.