groupon / node-cached

A simple caching library for node.js, inspired by the Play cache API
BSD 3-Clause "New" or "Revised" License
94 stars 15 forks source link

getOrElse missing in the latest version #52

Open advapiIT opened 3 years ago

advapiIT commented 3 years ago

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

Juraj-Masiar commented 3 years ago

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.