Cheapest way to cache data in node.js?

2019-07-20 15:07发布

I am implementing a node.js server running a matching algorithm. Since I want to keep the server responding as quickly as possible, I would like to be able to retrieve data from a cache instead of querying the database every single time. For example, I need to keep information about 10000 - 50000 users. My fear is that this would take up a lot of RAM. Is there any way to store that much data in a cache without the risk of running out of RAM?

4条回答
在下西门庆
2楼-- · 2019-07-20 15:17

You can use memcached or redis, who themselves care about memory and its lack removed obsolete data

查看更多
Luminary・发光体
3楼-- · 2019-07-20 15:20

node-memchache is faster than redis and is easier to implement. You can see https://github.com/ptarjan/node-cache

查看更多
狗以群分
4楼-- · 2019-07-20 15:29

from what I read as to what you are trying to do it seems you are contradicting yourself. AS to how I see, using cache has the following objectives:

  1. keep pre-processed data in memory so that you dont have to execute logic to generate them again
  2. keep data in a store which is much faster when we want to fetch the data.

Now, based on what I read from your question it seems your objective if #2. If that is so, then you have no option but to size for how much data do you want to keep in memory. That sizing should then be used to have enough RAM to hold the results and not going back to teh database.

Caching frameworks like ehCache also provide the option of swapping data from disk. Check this link from ehCache (http://www.ehcache.org/documentation/user-guide/storage-options#memory-use-spooling-and-expiry-strategy); this should help you design for how the cache should be setup aka eviction strategies to keep the sizes controllable and also when (and how) you should be ready to spool the data to a underlying disk-storage. Spooling to a disk means you would need to have HDD. But then you would need some benchmarking as to if DB is faster than the underlying cache-store.

you can chose in JVM caches like ehCachs, JCS or even go for much more sophisticated frameworks like memcache, redis which will do all this for you OOTB.

cheers kapil

查看更多
走好不送
5楼-- · 2019-07-20 15:30

With mcache it's fairly easy to cache data from database queries in Node.js.

Here is an example for caching a query from MySQL:

const Mcache = require('mcache');

const garbageCollectionInSeconds = 10;
const timeToLiveInSeconds = 60;

// This function will be called to update the cache
const update = function (id, callback) {
  mysql.query('SELECT ... WHERE id=?', id, callback);
};

const cache = new Mcache(timeToLiveInSeconds, garbageCollectionInSeconds, update);

const id = 'some-entity-id';
cache.get(id, function (error, data) {
  if (error) {
    console.error(error.message);
  } else {
    console.log(`Result: ${data}`);
  }
});
查看更多
登录 后发表回答