node-cached icon indicating copy to clipboard operation
node-cached copied to clipboard

getOrElse missing in the latest version

Open advapiIT opened this issue 4 years ago • 1 comments

Hello, It's the first time I use node-cache (I normally develop in c#), I've seen that's available on the doc the getOrElse method, I've tried with the following code but I got a getOrElse is not a myCache function error..

Here's the snippet of my class

const NodeCache = require( "node-cache" );
const myCache = new NodeCache();

var quadCache = {};

quadCache.setKey= function(key,obj, ttl)
{
    success = myCache.set( key, obj, ttl | 10000 );
    return success;
}

quadCache.getKey= function(key)
{
    value = myCache.get( key )

    if(value == undefined)
    {
        return null;
    }

    return value;

}

quadCache.getOrAdd = async function(key,funzione,ttl=20000)
{
    return myCache.getOrElse( key, await funzione(), ttl | 10000 );
}

module.exports = quadCache;

What am I doing wrong? Thanks

advapiIT avatar Nov 24 '20 16:11 advapiIT

You have a wrong import, it should be require('cached'). There is no constructor, so no such thing as new NodeCache();. Is that a bitwise OR??? ttl | 10000 :D, I think you meant to use ttl || 10000. Check the example code in the readme for simple usage of this library.

Juraj-Masiar avatar Jan 08 '21 10:01 Juraj-Masiar