Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS in-memory cache with memory pressure awareness

I'm coming from Java world, and there are plenty implementations of (local) in-memory caches. Moreover in Java world there are SoftReference and WeakReference, and they're, by definition, ideal for cache implementation(s).

I know that JavaScript does not have anything similar, so I'm wondering is it possible to have some sort of cache functionality which will delete/release (all) cached objects if there are "low memory pressure". So far, I know for lru-cache module, but it's implementation holds objects up to some number/size, which is nice, but not good enough because, naturally, you'd expect from cache to release objects if there are not enough memory.

Is it even possible to get some event in NodeJS from system when process is running low on memory?

Or maybe some library that could raise an event, something like:

var cmmm = require('cool_memory_management_module');

cmmm.on('low_memory', function(){
    //signaling to clear cache entries
});

So far, I've found npm memwatch, and npm usage modules, but still not able to combine all those pieces together.

like image 531
Tomo Avatar asked Jan 23 '14 14:01

Tomo


1 Answers

There are no WeakReferences or similar in JS yet, but shall come in ES6 (Version List).

So far, now you could build something that just checks every few seconds if the memory is running out and clean up your map.

setInterval(function() {
    /* check if memory low and do something */
}, 2000).unref();
like image 130
CFrei Avatar answered Nov 15 '22 17:11

CFrei