cache-manager icon indicating copy to clipboard operation
cache-manager copied to clipboard

cache error fallback

Open jpaolini opened this issue 10 years ago • 13 comments

If the call to the cache store fails. Should it fall back to the wrapped function?

jpaolini avatar Apr 22 '16 15:04 jpaolini

In caching.js this is controlled by passing an ignoreCacheErrors option: https://github.com/BryanDonovan/node-cache-manager/blob/master/lib/caching.js#L30

...but I just noticed that same option doesn't exist in multi_caching.js.

BryanDonovan avatar Apr 22 '16 16:04 BryanDonovan

I'll add this to multi_caching.js when I get some time (soon hopefully). Do you need it in multi_caching.js now, or are you just using regular caching.js?

BryanDonovan avatar Apr 27 '16 16:04 BryanDonovan

That worked perfectly. Right now I'm only using caching.js. Thank you such a quick reply.

jpaolini avatar Apr 27 '16 18:04 jpaolini

Does not fall back if redis is unavailable.

raj-krrish avatar Aug 24 '16 05:08 raj-krrish

ignoreCacheErrors not work with both of redis stores

mogadanez avatar May 21 '21 15:05 mogadanez

@mogadanez - do you mean when redis is unavailable?

BryanDonovan avatar May 21 '21 16:05 BryanDonovan

exactly, just provide unexisting url for redis.

mogadanez avatar May 21 '21 17:05 mogadanez

BTW, seems I found solution. enable_offline_queue: false for internal redis library

from redis doc:


enable_offline_queue | true | By default, if there is no active connection to the Redis server, commands are added to a queue and are executed once the connection has been established. Setting enable_offline_queue to false will disable this feature and the callback will be executed immediately with an error, or an error will be emitted if no callback is specified.

mogadanez avatar May 21 '21 17:05 mogadanez

Not sure if enable_offline_queue will work. It doesn't seem to prevent errors for me. Are you using async/await? If so, you can probably wrap your calls in a try/catch block. The issue here is that the ignoreCacheErrors only ignores errors returned via callbacks and doesn't deal with thrown errors. It would be difficult to wrap the internal caching.js calls with try/catch. But if you're using async/await, it shouldn't be too hard to do the try/catch in your code.

BryanDonovan avatar May 21 '21 17:05 BryanDonovan

when enable_offline_queue:false if goes inside if here:

https://github.com/NodeRedis/node-redis/blob/4f85030e42da2eed6a178e54994330af5062761e/index.js#L769

if enable_offline_queue:true https://github.com/NodeRedis/node-redis/blob/4f85030e42da2eed6a178e54994330af5062761e/index.js#L791 ( else case)

 self.offline_queue.push(command_obj);

and offline_queue will not proceed until ready status, so without this option, it never calls callback from cache manager, and completely not working( hangs )

RedisClient.prototype.on_ready = function () {
.....
    this.send_offline_queue();
    this.emit('ready');
}

my code now:

var redisStore = require('cache-manager-redis-store');

 let cacheImpl = cacheManager.caching({
        store: redisStore,
        host:"example.com", // default value
        enable_offline_queue:false,
        ignoreCacheErrors:true,
        db: 0,
        ttl: 6000,
        prefix: process.env.DATABASE_NAME
    });
....

  static async wrapQuery( key, query, params )
    {
        let res  = await cacheImpl.wrap(key, async ()=> {
            let [rows] = await  db.query(query, params);
            return rows
        });
       console.log( res )
        return res
    }

this works fine

mogadanez avatar May 21 '21 18:05 mogadanez

Awesome, glad you figured out a workaround.

BryanDonovan avatar May 21 '21 19:05 BryanDonovan

Thank you @mogadanez for sharing your solution. Unfortunately, we are using multicaching and the option ignoreCacheErrors doesn't seem supported. @BryanDonovan Is there any plan to implement ignoreCacheErrors in multicaching too? Thank you for this package.

umbobabo avatar Mar 08 '22 15:03 umbobabo

@umbobabo - Sorry, I don't have time to add any features now, but I would accept a pull request.

BryanDonovan avatar Mar 08 '22 17:03 BryanDonovan