Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching database queries with Node.js

Is there an implementation of database (mysql) query caching written purely in Node.js?

I'm writing a Node web app and was planning on caching queries with memcached, but while considering this I realised it's probably possible to do the caching through a separate Node.js layer instead

To explain:

You could query the database through a node server on a separate port, returning data from memory where available and loading it into memory where it isn't.

Anyone know how Node.js would compare to memcache in terms of return speed on hashed arrays? Is this a pipe-dream or something I should look at?

like image 440
thelastshadow Avatar asked Feb 09 '12 16:02

thelastshadow


3 Answers

I went ahead and wrote a caching solution for private use that stored the data in a shared object. This wasn't really query caching, it stores specific results instead of raw sql results ordered by hashes, but it kept what I needed in memory and was ridiculously easy to write.

Since I originally asked this question a number of node caching solutions have emmerged:

  1. ptarjan/node-cache
  2. tcs-de/nodecache
  3. vxtindia/node-cache
  4. mape/node-caching

I haven't used any of these but one of them might well be of use to someone else.

There are now also redis and memcached clients for node.

like image 81
thelastshadow Avatar answered Oct 18 '22 20:10

thelastshadow


You can definitely implement something like this in node, and it could be an interesting project, but it depends on your needs. If you're just doing this for a hobby project, by all means, build a caching layer in node and try it out. Let us know how it goes!

If this is for production use, then I would recommend sticking to the established caching layers (memcached, redis, etc) as they have already gone through all of the growing pains associated with building a scalable caching system.

like image 26
Timothy Strimple Avatar answered Oct 18 '22 20:10

Timothy Strimple


I have written a node.js module that performs MySQL query caching using memcached.

The module is named Memento and is available at https://www.npmjs.com/package/memento-mysql

Enjoy!

like image 1
user3280601 Avatar answered Oct 18 '22 20:10

user3280601