Use redis 2 directly.

This commit is contained in:
Julien Fontanet 2015-10-05 15:32:34 +02:00
parent 98b2b325a1
commit 1604d327da
3 changed files with 22 additions and 13 deletions

View File

@ -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",

View File

@ -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))
])
}

View File

@ -99,6 +99,7 @@ export const pFinally = (promise, cb) => {
// -------------------------------------------------------------------
export {promisify}
export {promisifyAll} from 'bluebird'
// -------------------------------------------------------------------