Cheapest Way To Cache Data In Node.js?
Solution 1:
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:
- keep pre-processed data in memory so that you dont have to execute logic to generate them again
- 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
Solution 2:
You can use memcached or redis, who themselves care about memory and its lack removed obsolete data
Solution 3:
node-memchache is faster than redis and is easier to implement. You can see https://github.com/ptarjan/node-cache
Solution 4:
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:
constMcache = require('mcache');
const garbageCollectionInSeconds = 10;
const timeToLiveInSeconds = 60;
// This function will be called to update the cacheconst update = function (id, callback) {
mysql.query('SELECT ... WHERE id=?', id, callback);
};
const cache = newMcache(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}`);
}
});
Post a Comment for "Cheapest Way To Cache Data In Node.js?"