Caching database queries with Node.js

2019-02-12 17:01发布

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?

3条回答
放荡不羁爱自由
2楼-- · 2019-02-12 17:33

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.

查看更多
贼婆χ
3楼-- · 2019-02-12 17:39

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!

查看更多
狗以群分
4楼-- · 2019-02-12 17:46

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.

查看更多
登录 后发表回答