dial-once/node-cache-manager-redis

Promises missing?

angelxmoreno opened this issue · 5 comments

Not sure if I am doing something wrong in my setup, but when I try to use promises in multicache, I don't ever get the cache data.

Is this adapter not compatible with promises?

@angelxmoreno This lib is not yet promise ready.

As a side note, because I'm not sure to understand what is your issue:

When you use directly node-cache-manager-redis, you can't use the exposed methods as promises (.get, .set, .ttl, etc.).

However if you are using node-cache-manager, and the multicache feature (since you mentioned it), promises should be handled for you since version 1.3 of node-cache-manager: jaredwray/cacheable#39 (as you can see we did the PR and tested it against this module, so it should work)

And you can do .wrap, .get, etc. on the multicache object and expect a promise as return value of those methods.

To fully understand your issue, can you provide your node-cache-manager version, as well as a sample code that is failing?

Also, I recently had unexpected behaviour with node-cache-manager and multicache when using node-cache-manager version > 1.5, having some timeouts as you described.
Can you try to downgrade to version 1.3 or 1.5 and check if you still experience the issue? If so, we have to update this module as soon as possible.

ty. will test soon

For anyone looking to use promises, I have found using bluebird to be the easiest solution. Here is an example:

const cacheManager = require('cache-manager'),
      redisStore   = require('cache-manager-redis'),
      Promise      = require('bluebird');

const cache = cacheManager.caching({
    store: redisStore,
    host: config.host,
    port: config.port,
    ttl: config.ttl,
    promiseDependency: Promise
});

Promise.promisifyAll(cache);

cache.store.getClientAsync = Promise.promisify(cache.store.getClient, { context: cache.store });

This creates async versions of all the available methods with an Async suffix.

cache.setAsync('foo', 'bar').then(function(result) { });

etc... Or, even better, if you are using generators:

let value = yield cache.getAsync('foo');

You can even promisify individual methods of the underlying redis client with something like this:

cache.store.getClient(function (err, redis) {
    redis.client.mgetAsync = Promise.promisify(redis.client.mget, { context: redis.client });
    redis.client.msetAsync = Promise.promisify(redis.client.mset, { context: redis.client });
    redis.done();
});

Although the update to sol-redis-pool 3.x is causing some errors with this, now. I am currently trying to get the author of that package to add bluebird support, so all clients in the underlying pool are promisified automatically. (If you would also like to see this, you can add your comments / support to the issue I created here.)

closing in favor of @jeff-kilbride snippet