From 1604d327dad9c1254f749a798f1ad40806be5630 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 5 Oct 2015 15:32:34 +0200 Subject: [PATCH] Use redis 2 directly. --- package.json | 3 +-- src/collection/redis.js | 31 ++++++++++++++++++++----------- src/utils.js | 1 + 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 8aa0538b2..d5a28509c 100644 --- a/package.json +++ b/package.json @@ -85,12 +85,11 @@ "passport": "^0.3.0", "passport-local": "^1.0.0", "proxy-http-request": "0.1.0", - "redis": "^0.12.1", + "redis": "^2.0.1", "request": "^2.53.0", "schema-inspector": "^1.5.1", "serve-static": "^1.9.2", "source-map-support": "^0.3.2", - "then-redis": "~1.3.0", "trace": "^1.2.0", "ws": "~0.8.0", "xen-api": "^0.6.1", diff --git a/src/collection/redis.js b/src/collection/redis.js index 55d413a19..c24833a68 100644 --- a/src/collection/redis.js +++ b/src/collection/redis.js @@ -6,7 +6,16 @@ import forEach from 'lodash.foreach' import getKey from 'lodash.keys' import isEmpty from 'lodash.isempty' import map from 'lodash.map' -import thenRedis from 'then-redis' +import {createClient as createRedisClient, RedisClient, Multi} from 'redis' + +import {promisifyAll} from '../utils' + +// =================================================================== + +promisifyAll(RedisClient.prototype) +promisifyAll(Multi.prototype) + +// =================================================================== // /////////////////////////////////////////////////////////////////// // Data model: @@ -36,7 +45,7 @@ export default class Redis extends Collection { this.indexes = indexes this.prefix = prefix - this.redis = connection || thenRedis.createClient(uri) + this.redis = connection || createRedisClient(uri) } _extract (ids) { @@ -45,7 +54,7 @@ export default class Redis extends Collection { const models = [] return Bluebird.map(ids, id => { - return redis.hgetall(prefix + id).then(model => { + return redis.hgetallAsync(prefix + id).then(model => { // If empty, consider it a no match. if (isEmpty(model)) { return @@ -68,10 +77,10 @@ export default class Redis extends Collection { return Bluebird.map(models, async function (model) { // Generate a new identifier if necessary. if (model.id === undefined) { - model.id = idPrefix + String(await redis.incr(prefix + '_id')) + model.id = idPrefix + String(await redis.incrAsync(prefix + '_id')) } - const success = await redis.sadd(prefix + '_ids', model.id) + const success = await redis.saddAsync(prefix + '_ids', model.id) // The entry already exists an we are not in replace mode. if (!success && !replace) { @@ -91,7 +100,7 @@ export default class Redis extends Collection { }) const promises = [ - redis.hmset(prefix + ':' + model.id, ...params) + redis.hmsetAsync(prefix + ':' + model.id, ...params) ] // Update indexes. @@ -102,7 +111,7 @@ export default class Redis extends Collection { } const key = prefix + '_' + index + ':' + value - promises.push(redis.sadd(key, model.id)) + promises.push(redis.saddAsync(key, model.id)) }) await Promise.all(promises) @@ -115,7 +124,7 @@ export default class Redis extends Collection { const {prefix, redis} = this if (isEmpty(properties)) { - return redis.smembers(prefix + '_ids').then(ids => this._extract(ids)) + return redis.smembersAsync(prefix + '_ids').then(ids => this._extract(ids)) } // Special treatment for the identifier. @@ -138,7 +147,7 @@ export default class Redis extends Collection { } const keys = map(properties, (value, index) => prefix + '_' + index + ':' + value) - return redis.sinter(...keys).then(ids => this._extract(ids)) + return redis.sinterAsync(...keys).then(ids => this._extract(ids)) } _remove (ids) { @@ -148,10 +157,10 @@ export default class Redis extends Collection { return Promise.all([ // Remove the identifiers from the main index. - redis.srem(prefix + '_ids', ...ids), + redis.sremAsync(prefix + '_ids', ...ids), // Remove the models. - redis.del(map(ids, id => prefix + ':' + id)) + redis.delAsync(map(ids, id => prefix + ':' + id)) ]) } diff --git a/src/utils.js b/src/utils.js index 951f28105..d8341bb4d 100644 --- a/src/utils.js +++ b/src/utils.js @@ -99,6 +99,7 @@ export const pFinally = (promise, cb) => { // ------------------------------------------------------------------- export {promisify} +export {promisifyAll} from 'bluebird' // -------------------------------------------------------------------