mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 03:01:08 +01:00
refactor: remove promisify from redis, ioredis supports promises nati… (#9728)
* refactor: remove promisify from redis, ioredis supports promises natively * refactor: remove unused util
This commit is contained in:
committed by
GitHub
parent
d44e3a8ecf
commit
6659e95a4a
@@ -2,7 +2,6 @@
|
||||
|
||||
const nconf = require('nconf');
|
||||
const semver = require('semver');
|
||||
const util = require('util');
|
||||
const session = require('express-session');
|
||||
|
||||
const connection = require('./redis/connection');
|
||||
@@ -37,7 +36,6 @@ redisModule.questions = [
|
||||
|
||||
redisModule.init = async function () {
|
||||
redisModule.client = await connection.connect(nconf.get('redis'));
|
||||
require('./redis/promisify')(redisModule.client);
|
||||
};
|
||||
|
||||
redisModule.createSessionStore = async function (options) {
|
||||
@@ -64,7 +62,7 @@ redisModule.checkCompatibilityVersion = function (version, callback) {
|
||||
};
|
||||
|
||||
redisModule.close = async function () {
|
||||
await redisModule.client.async.quit();
|
||||
await redisModule.client.quit();
|
||||
};
|
||||
|
||||
redisModule.info = async function (cxn) {
|
||||
@@ -72,8 +70,7 @@ redisModule.info = async function (cxn) {
|
||||
cxn = await connection.connect(nconf.get('redis'));
|
||||
}
|
||||
redisModule.client = redisModule.client || cxn;
|
||||
const infoAsync = util.promisify(cb => cxn.info(cb));
|
||||
const data = await infoAsync();
|
||||
const data = await cxn.info();
|
||||
const lines = data.toString().split('\r\n').sort();
|
||||
const redisData = {};
|
||||
lines.forEach((line) => {
|
||||
|
||||
@@ -30,7 +30,7 @@ module.exports = function (module) {
|
||||
key.forEach(k => batch.hmset(k, data));
|
||||
await helpers.execBatch(batch);
|
||||
} else {
|
||||
await module.client.async.hmset(key, data);
|
||||
await module.client.hmset(key, data);
|
||||
}
|
||||
|
||||
cache.del(key);
|
||||
@@ -55,7 +55,7 @@ module.exports = function (module) {
|
||||
key.forEach(k => batch.hset(k, field, value));
|
||||
await helpers.execBatch(batch);
|
||||
} else {
|
||||
await module.client.async.hset(key, field, value);
|
||||
await module.client.hset(key, field, value);
|
||||
}
|
||||
|
||||
cache.del(key);
|
||||
@@ -83,7 +83,7 @@ module.exports = function (module) {
|
||||
if (cachedData[key]) {
|
||||
return cachedData[key].hasOwnProperty(field) ? cachedData[key][field] : null;
|
||||
}
|
||||
return await module.client.async.hget(key, String(field));
|
||||
return await module.client.hget(key, String(field));
|
||||
};
|
||||
|
||||
module.getObjectFields = async function (key, fields) {
|
||||
@@ -108,7 +108,7 @@ module.exports = function (module) {
|
||||
unCachedKeys.forEach(k => batch.hgetall(k));
|
||||
data = await helpers.execBatch(batch);
|
||||
} else if (unCachedKeys.length === 1) {
|
||||
data = [await module.client.async.hgetall(unCachedKeys[0])];
|
||||
data = [await module.client.hgetall(unCachedKeys[0])];
|
||||
}
|
||||
|
||||
// convert empty objects into null for back-compat with node_redis
|
||||
@@ -138,15 +138,15 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.getObjectKeys = async function (key) {
|
||||
return await module.client.async.hkeys(key);
|
||||
return await module.client.hkeys(key);
|
||||
};
|
||||
|
||||
module.getObjectValues = async function (key) {
|
||||
return await module.client.async.hvals(key);
|
||||
return await module.client.hvals(key);
|
||||
};
|
||||
|
||||
module.isObjectField = async function (key, field) {
|
||||
const exists = await module.client.async.hexists(key, field);
|
||||
const exists = await module.client.hexists(key, field);
|
||||
return exists === 1;
|
||||
};
|
||||
|
||||
@@ -161,7 +161,7 @@ module.exports = function (module) {
|
||||
if (key === undefined || key === null || field === undefined || field === null) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.hdel(key, field);
|
||||
await module.client.hdel(key, field);
|
||||
cache.del(key);
|
||||
};
|
||||
|
||||
@@ -178,7 +178,7 @@ module.exports = function (module) {
|
||||
key.forEach(k => batch.hdel(k, fields));
|
||||
await helpers.execBatch(batch);
|
||||
} else {
|
||||
await module.client.async.hdel(key, fields);
|
||||
await module.client.hdel(key, fields);
|
||||
}
|
||||
|
||||
cache.del(key);
|
||||
@@ -203,7 +203,7 @@ module.exports = function (module) {
|
||||
key.forEach(k => batch.hincrby(k, field, value));
|
||||
result = await helpers.execBatch(batch);
|
||||
} else {
|
||||
result = await module.client.async.hincrby(key, field, value);
|
||||
result = await module.client.hincrby(key, field, value);
|
||||
}
|
||||
cache.del(key);
|
||||
return Array.isArray(result) ? result.map(value => parseInt(value, 10)) : parseInt(result, 10);
|
||||
|
||||
@@ -5,45 +5,45 @@ module.exports = function (module) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.lpush(key, value);
|
||||
await module.client.lpush(key, value);
|
||||
};
|
||||
|
||||
module.listAppend = async function (key, value) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.rpush(key, value);
|
||||
await module.client.rpush(key, value);
|
||||
};
|
||||
|
||||
module.listRemoveLast = async function (key) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
return await module.client.async.rpop(key);
|
||||
return await module.client.rpop(key);
|
||||
};
|
||||
|
||||
module.listRemoveAll = async function (key, value) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.lrem(key, 0, value);
|
||||
await module.client.lrem(key, 0, value);
|
||||
};
|
||||
|
||||
module.listTrim = async function (key, start, stop) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.ltrim(key, start, stop);
|
||||
await module.client.ltrim(key, start, stop);
|
||||
};
|
||||
|
||||
module.getListRange = async function (key, start, stop) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
return await module.client.async.lrange(key, start, stop);
|
||||
return await module.client.lrange(key, start, stop);
|
||||
};
|
||||
|
||||
module.listLength = async function (key) {
|
||||
return await module.client.async.llen(key);
|
||||
return await module.client.llen(key);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@ module.exports = function (module) {
|
||||
const helpers = require('./helpers');
|
||||
|
||||
module.flushdb = async function () {
|
||||
await module.client.async.send_command('flushdb', []);
|
||||
await module.client.send_command('flushdb', []);
|
||||
};
|
||||
|
||||
module.emptydb = async function () {
|
||||
@@ -19,7 +19,7 @@ module.exports = function (module) {
|
||||
const data = await helpers.execBatch(batch);
|
||||
return data.map(exists => exists === 1);
|
||||
}
|
||||
const exists = await module.client.async.exists(key);
|
||||
const exists = await module.client.exists(key);
|
||||
return exists === 1;
|
||||
};
|
||||
|
||||
@@ -29,7 +29,7 @@ module.exports = function (module) {
|
||||
const seen = {};
|
||||
do {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const res = await module.client.async.scan(cursor, 'MATCH', params.match, 'COUNT', 10000);
|
||||
const res = await module.client.scan(cursor, 'MATCH', params.match, 'COUNT', 10000);
|
||||
cursor = res[0];
|
||||
const values = res[1].filter((value) => {
|
||||
const isSeen = !!seen[value];
|
||||
@@ -44,7 +44,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.delete = async function (key) {
|
||||
await module.client.async.del(key);
|
||||
await module.client.del(key);
|
||||
module.objectCache.del(key);
|
||||
};
|
||||
|
||||
@@ -52,25 +52,25 @@ module.exports = function (module) {
|
||||
if (!Array.isArray(keys) || !keys.length) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.del(keys);
|
||||
await module.client.del(keys);
|
||||
module.objectCache.del(keys);
|
||||
};
|
||||
|
||||
module.get = async function (key) {
|
||||
return await module.client.async.get(key);
|
||||
return await module.client.get(key);
|
||||
};
|
||||
|
||||
module.set = async function (key, value) {
|
||||
await module.client.async.set(key, value);
|
||||
await module.client.set(key, value);
|
||||
};
|
||||
|
||||
module.increment = async function (key) {
|
||||
return await module.client.async.incr(key);
|
||||
return await module.client.incr(key);
|
||||
};
|
||||
|
||||
module.rename = async function (oldKey, newKey) {
|
||||
try {
|
||||
await module.client.async.rename(oldKey, newKey);
|
||||
await module.client.rename(oldKey, newKey);
|
||||
} catch (err) {
|
||||
if (err && err.message !== 'ERR no such key') {
|
||||
throw err;
|
||||
@@ -81,31 +81,31 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.type = async function (key) {
|
||||
const type = await module.client.async.type(key);
|
||||
const type = await module.client.type(key);
|
||||
return type !== 'none' ? type : null;
|
||||
};
|
||||
|
||||
module.expire = async function (key, seconds) {
|
||||
await module.client.async.expire(key, seconds);
|
||||
await module.client.expire(key, seconds);
|
||||
};
|
||||
|
||||
module.expireAt = async function (key, timestamp) {
|
||||
await module.client.async.expireat(key, timestamp);
|
||||
await module.client.expireat(key, timestamp);
|
||||
};
|
||||
|
||||
module.pexpire = async function (key, ms) {
|
||||
await module.client.async.pexpire(key, ms);
|
||||
await module.client.pexpire(key, ms);
|
||||
};
|
||||
|
||||
module.pexpireAt = async function (key, timestamp) {
|
||||
await module.client.async.pexpireat(key, timestamp);
|
||||
await module.client.pexpireat(key, timestamp);
|
||||
};
|
||||
|
||||
module.ttl = async function (key) {
|
||||
return await module.client.async.ttl(key);
|
||||
return await module.client.ttl(key);
|
||||
};
|
||||
|
||||
module.pttl = async function (key) {
|
||||
return await module.client.async.pttl(key);
|
||||
return await module.client.pttl(key);
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const util = require('util');
|
||||
|
||||
module.exports = function (redisClient) {
|
||||
redisClient.async = {
|
||||
quit: util.promisify(redisClient.quit).bind(redisClient),
|
||||
send_command: util.promisify(redisClient.send_command).bind(redisClient),
|
||||
|
||||
exists: util.promisify(redisClient.exists).bind(redisClient),
|
||||
scan: util.promisify(redisClient.scan).bind(redisClient),
|
||||
|
||||
del: util.promisify(redisClient.del).bind(redisClient),
|
||||
get: util.promisify(redisClient.get).bind(redisClient),
|
||||
set: util.promisify(redisClient.set).bind(redisClient),
|
||||
incr: util.promisify(redisClient.incr).bind(redisClient),
|
||||
rename: util.promisify(redisClient.rename).bind(redisClient),
|
||||
type: util.promisify(redisClient.type).bind(redisClient),
|
||||
expire: util.promisify(redisClient.expire).bind(redisClient),
|
||||
expireat: util.promisify(redisClient.expireat).bind(redisClient),
|
||||
pexpire: util.promisify(redisClient.pexpire).bind(redisClient),
|
||||
pexpireat: util.promisify(redisClient.pexpireat).bind(redisClient),
|
||||
ttl: util.promisify(redisClient.ttl).bind(redisClient),
|
||||
pttl: util.promisify(redisClient.pttl).bind(redisClient),
|
||||
|
||||
hmset: util.promisify(redisClient.hmset).bind(redisClient),
|
||||
hset: util.promisify(redisClient.hset).bind(redisClient),
|
||||
hget: util.promisify(redisClient.hget).bind(redisClient),
|
||||
hdel: util.promisify(redisClient.hdel).bind(redisClient),
|
||||
hgetall: util.promisify(redisClient.hgetall).bind(redisClient),
|
||||
hkeys: util.promisify(redisClient.hkeys).bind(redisClient),
|
||||
hvals: util.promisify(redisClient.hvals).bind(redisClient),
|
||||
hexists: util.promisify(redisClient.hexists).bind(redisClient),
|
||||
hincrby: util.promisify(redisClient.hincrby).bind(redisClient),
|
||||
|
||||
sadd: util.promisify(redisClient.sadd).bind(redisClient),
|
||||
srem: util.promisify(redisClient.srem).bind(redisClient),
|
||||
sismember: util.promisify(redisClient.sismember).bind(redisClient),
|
||||
smembers: util.promisify(redisClient.smembers).bind(redisClient),
|
||||
scard: util.promisify(redisClient.scard).bind(redisClient),
|
||||
spop: util.promisify(redisClient.spop).bind(redisClient),
|
||||
|
||||
zadd: util.promisify(redisClient.zadd).bind(redisClient),
|
||||
zrem: util.promisify(redisClient.zrem).bind(redisClient),
|
||||
zrange: util.promisify(redisClient.zrange).bind(redisClient),
|
||||
zrevrange: util.promisify(redisClient.zrevrange).bind(redisClient),
|
||||
zrangebyscore: util.promisify(redisClient.zrangebyscore).bind(redisClient),
|
||||
zrevrangebyscore: util.promisify(redisClient.zrevrangebyscore).bind(redisClient),
|
||||
zscore: util.promisify(redisClient.zscore).bind(redisClient),
|
||||
zcount: util.promisify(redisClient.zcount).bind(redisClient),
|
||||
zcard: util.promisify(redisClient.zcard).bind(redisClient),
|
||||
zrank: util.promisify(redisClient.zrank).bind(redisClient),
|
||||
zrevrank: util.promisify(redisClient.zrevrank).bind(redisClient),
|
||||
zincrby: util.promisify(redisClient.zincrby).bind(redisClient),
|
||||
zrangebylex: util.promisify(redisClient.zrangebylex).bind(redisClient),
|
||||
zrevrangebylex: util.promisify(redisClient.zrevrangebylex).bind(redisClient),
|
||||
zremrangebylex: util.promisify(redisClient.zremrangebylex).bind(redisClient),
|
||||
zlexcount: util.promisify(redisClient.zlexcount).bind(redisClient),
|
||||
zscan: util.promisify(redisClient.zscan).bind(redisClient),
|
||||
|
||||
lpush: util.promisify(redisClient.lpush).bind(redisClient),
|
||||
rpush: util.promisify(redisClient.rpush).bind(redisClient),
|
||||
rpop: util.promisify(redisClient.rpop).bind(redisClient),
|
||||
lrem: util.promisify(redisClient.lrem).bind(redisClient),
|
||||
ltrim: util.promisify(redisClient.ltrim).bind(redisClient),
|
||||
lrange: util.promisify(redisClient.lrange).bind(redisClient),
|
||||
llen: util.promisify(redisClient.llen).bind(redisClient),
|
||||
|
||||
};
|
||||
};
|
||||
@@ -10,7 +10,7 @@ module.exports = function (module) {
|
||||
if (!value.length) {
|
||||
return;
|
||||
}
|
||||
await module.client.async.sadd(key, value);
|
||||
await module.client.sadd(key, value);
|
||||
};
|
||||
|
||||
module.setsAdd = async function (keys, value) {
|
||||
@@ -45,7 +45,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.isSetMember = async function (key, value) {
|
||||
const result = await module.client.async.sismember(key, value);
|
||||
const result = await module.client.sismember(key, value);
|
||||
return result === 1;
|
||||
};
|
||||
|
||||
@@ -64,7 +64,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.getSetMembers = async function (key) {
|
||||
return await module.client.async.smembers(key);
|
||||
return await module.client.smembers(key);
|
||||
};
|
||||
|
||||
module.getSetsMembers = async function (keys) {
|
||||
@@ -74,7 +74,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.setCount = async function (key) {
|
||||
return await module.client.async.scard(key);
|
||||
return await module.client.scard(key);
|
||||
};
|
||||
|
||||
module.setsCount = async function (keys) {
|
||||
@@ -84,7 +84,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.setRemoveRandom = async function (key) {
|
||||
return await module.client.async.spop(key);
|
||||
return await module.client.spop(key);
|
||||
};
|
||||
|
||||
return module;
|
||||
|
||||
@@ -49,7 +49,7 @@ module.exports = function (module) {
|
||||
}
|
||||
|
||||
const params = genParams(method, key, start, stop, min, max, withScores);
|
||||
const data = await module.client.async[method](params);
|
||||
const data = await module.client[method](params);
|
||||
if (!withScores) {
|
||||
return data;
|
||||
}
|
||||
@@ -100,11 +100,11 @@ module.exports = function (module) {
|
||||
}
|
||||
|
||||
module.sortedSetCount = async function (key, min, max) {
|
||||
return await module.client.async.zcount(key, min, max);
|
||||
return await module.client.zcount(key, min, max);
|
||||
};
|
||||
|
||||
module.sortedSetCard = async function (key) {
|
||||
return await module.client.async.zcard(key);
|
||||
return await module.client.zcard(key);
|
||||
};
|
||||
|
||||
module.sortedSetsCard = async function (keys) {
|
||||
@@ -129,11 +129,11 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.sortedSetRank = async function (key, value) {
|
||||
return await module.client.async.zrank(key, value);
|
||||
return await module.client.zrank(key, value);
|
||||
};
|
||||
|
||||
module.sortedSetRevRank = async function (key, value) {
|
||||
return await module.client.async.zrevrank(key, value);
|
||||
return await module.client.zrevrank(key, value);
|
||||
};
|
||||
|
||||
module.sortedSetsRanks = async function (keys, values) {
|
||||
@@ -173,7 +173,7 @@ module.exports = function (module) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const score = await module.client.async.zscore(key, value);
|
||||
const score = await module.client.zscore(key, value);
|
||||
return score === null ? score : parseFloat(score);
|
||||
};
|
||||
|
||||
@@ -223,7 +223,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.getSortedSetMembers = async function (key) {
|
||||
return await module.client.async.zrange(key, 0, -1);
|
||||
return await module.client.zrange(key, 0, -1);
|
||||
};
|
||||
|
||||
module.getSortedSetsMembers = async function (keys) {
|
||||
@@ -236,7 +236,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.sortedSetIncrBy = async function (key, increment, value) {
|
||||
const newValue = await module.client.async.zincrby(key, increment, value);
|
||||
const newValue = await module.client.zincrby(key, increment, value);
|
||||
return parseFloat(newValue);
|
||||
};
|
||||
|
||||
@@ -277,7 +277,7 @@ module.exports = function (module) {
|
||||
if (count) {
|
||||
args.push('LIMIT', start, count);
|
||||
}
|
||||
return await module.client.async[method](args);
|
||||
return await module.client[method](args);
|
||||
}
|
||||
|
||||
module.getSortedSetScan = async function (params) {
|
||||
@@ -288,7 +288,7 @@ module.exports = function (module) {
|
||||
const seen = {};
|
||||
do {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const res = await module.client.async.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 5000);
|
||||
const res = await module.client.zscan(params.key, cursor, 'MATCH', params.match, 'COUNT', 5000);
|
||||
cursor = res[0];
|
||||
done = cursor === '0';
|
||||
const data = res[1];
|
||||
|
||||
@@ -14,7 +14,7 @@ module.exports = function (module) {
|
||||
if (!utils.isNumber(score)) {
|
||||
throw new Error(`[[error:invalid-score, ${score}]]`);
|
||||
}
|
||||
await module.client.async.zadd(key, score, String(value));
|
||||
await module.client.zadd(key, score, String(value));
|
||||
};
|
||||
|
||||
async function sortedSetAddMulti(key, scores, values) {
|
||||
@@ -34,7 +34,7 @@ module.exports = function (module) {
|
||||
for (let i = 0; i < scores.length; i += 1) {
|
||||
args.push(scores[i], String(values[i]));
|
||||
}
|
||||
await module.client.async.zadd(args);
|
||||
await module.client.zadd(args);
|
||||
}
|
||||
|
||||
module.sortedSetsAdd = async function (keys, scores, value) {
|
||||
|
||||
@@ -21,7 +21,7 @@ module.exports = function (module) {
|
||||
key.forEach(k => batch.zrem(k, value));
|
||||
await helpers.execBatch(batch);
|
||||
} else {
|
||||
await module.client.async.zrem(key, value);
|
||||
await module.client.zrem(key, value);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user