Nodejs cache images or other binary data on local disk filesystem

I’ve been looking for a simple npm package that will allow caching arbitrary binary data (images, specifically) on a local fs storage. I could have do the work for saving the files locally, but cache invalidation task was too much. In a hope that someone already created the package I’ve looked through the npm. What I’ve found suitable at the first glance was the cache-manager-fs npm package. And I embedded it into the project. The wrap function that the cache-manager provides is of a great convenience. Thanks to the authors.

So, the task I needed cache for is generating a collage by downloading userpics, putting them on a background and adding some text. Downloading is a time consuming operation, while generating the image is CPU consuming. It is good to cache both, since the disk space is relatively cheap, same userpics could be used to generate different collages and we want a low HTTP respond time since the collages should be served, not to mention the CPU load.

After I’ve embedded and tested local version I’ve noticed that while the generation operations takes about 1s wall time for the whole request, the cached version takes about 0.6s wall time. Still it was less time and traffic and I deployed the version.

While embedding I’ve noticed that the cached object is stored as JSON representation and binary data, which have special type Buffer, are turned into objects with two fields: type and data. Type is a string with `buffer` content, while the data are stored as an array of bytes, which in JSON form may take up to 6 times the size of an original buffer. So, parsing Buffers to JSON forth and back is both time and CPU consuming. That had to be fixed. I’ve decided to go with storing binary data in separate files. Doing so we also can take an advantage of streams, which are better for HTTP response since they’re less memory consuming.

So I came up with a new storage engine for the cache-managercache-manager-fs-binary, based on the cache-manager-fs.

Concerning the impact. We have not much load yet, so the CPU graph has not changed significantly. The least response time for the cached collage request dropped from 0.6s to 0.008s, while the average stays around 0.06s. Much better.

So, if you’re looking for a simple cache of binary data both with arbitrary object data, cache-manager-fs-binary is the way to go.

There’s still much to be improved, though. Comment here or drop issues or PRs at the github repo.

  • Facebook
  • Twitter
  • VKontakte
  • Google Plus
  • Delicious
  • StumbleUpon
  • Add to favorites