cache error fallback
If the call to the cache store fails. Should it fall back to the wrapped function?
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.
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?
That worked perfectly. Right now I'm only using caching.js. Thank you such a quick reply.
Does not fall back if redis is unavailable.
ignoreCacheErrors not work with both of redis stores
@mogadanez - do you mean when redis is unavailable?
exactly, just provide unexisting url for redis.
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.
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.
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
Awesome, glad you figured out a workaround.
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 - Sorry, I don't have time to add any features now, but I would accept a pull request.