node-cache / node-cache

a node internal (in-memory) caching module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fails to cache Promises properly

matthieusieben opened this issue · comments

Promises (available in Node 0.12+) should not be cloned (or the clone module should handle Promises properly).
Currently, nodecache breaks implementations caching promises.

can you provide a example or test case?

var NodeCache = require( "node-cache" );
var myCache = new NodeCache();
var p = new Promise(function(fulfill, reject) { fulfill('Some deferred value'); });

p.then(function(value) {
    console.log('promise result', value);
});

myCache.set('p', p);

var q = myCache.get('p');

try {
    q.then(function(value) {
        console.log('cached promise result', value);
    });
} catch(e) {
    console.error('Unable to treat q as a promise', e);
}

I just saw that 2.2.0 is a compatibility breaking update. Don't forget that the versioning of npm projects should respect : http://semver.org/ (i.e. not break compatibility when the first number does not change).

Most people having "node-cache": "^2.1.1" may encounter this kind of problem which is not what you want.

For now I just fixed my problem using "node-cache": "2.1.1" as depedency.

Also, note that cloning cached items may not be what users want. You should expose this as an option.

The cloning of items should be the default. Version 2.x should work as before with the default being not to clone. Version 3.x should have cloning disabled by default with an option to disable it.

i unpublished the version 2.2.0. So it's not possible to install it any more.
Currently i'm testing it as version 3.0.0 with a new option useClones:false.

Testing done. The version 3.0.0is online.

Does 3.0.0 with useClones: true works with promises ? I guess the expected behavior would be to create a new Promise that contains a cloned version of the fulfilled value/rejected reason.

i don't think this will work.
If you want to cache promises use useClones: false as i described within the options docs

I'm using this module node-clone to clone variables, because i won't implement such a basic function again :-).
Of course you could open an issue in node-clone for a correct clone of a promise.

Yep, that would make more sense.
pvorb/clone#50