Files
NodeBB/src/database/redis/main.js
Barış Uşaklı 14043ab0fd Node redis (#13500)
* refactor: start migrating to node-redis

* few more zset fixes

* fix: db.scan

* fix: list methods

* fix set methods

* fix: hash methods

* use hasOwn, remove cloning

* sorted set fixes

* fix: so data is converted to strings before saving

otherwise node-redis throws below error
TypeError: "arguments[2]" must be of type "string | Buffer", got number instead.

* chore: remove comments

* fix: zrank string param

* use new close

* chore: up dbsearch

* test: add log

* test: more log

* test: log failing test

* test: catch errors in formatApiResponse

add await so exception goes to catch

* tetst: add log

* fix: dont set null/undefined values

* test: more fixes
2025-06-18 13:04:57 -04:00

122 lines
2.8 KiB
JavaScript

'use strict';
module.exports = function (module) {
const helpers = require('./helpers');
module.flushdb = async function () {
await module.client.sendCommand(['FLUSHDB']);
};
module.emptydb = async function () {
await module.flushdb();
module.objectCache.reset();
};
module.exists = async function (key) {
if (Array.isArray(key)) {
if (!key.length) {
return [];
}
const batch = module.client.batch();
key.forEach(key => batch.exists(key));
const data = await helpers.execBatch(batch);
return data.map(exists => exists === 1);
}
const exists = await module.client.exists(key);
return exists === 1;
};
module.scan = async function (params) {
let cursor = '0';
let returnData = [];
const seen = Object.create(null);
do {
/* eslint-disable no-await-in-loop */
const res = await module.client.scan(cursor, { MATCH: params.match, COUNT: 10000 });
cursor = res.cursor;
const values = res.keys.filter((value) => {
const isSeen = !!seen[value];
if (!isSeen) {
seen[value] = 1;
}
return !isSeen;
});
returnData = returnData.concat(values);
} while (cursor !== '0');
return returnData;
};
module.delete = async function (key) {
await module.client.del(key);
module.objectCache.del(key);
};
module.deleteAll = async function (keys) {
if (!Array.isArray(keys) || !keys.length) {
return;
}
await module.client.del(keys);
module.objectCache.del(keys);
};
module.get = async function (key) {
return await module.client.get(key);
};
module.mget = async function (keys) {
if (!keys || !Array.isArray(keys) || !keys.length) {
return [];
}
return await module.client.mGet(keys);
};
module.set = async function (key, value) {
await module.client.set(key, value);
};
module.increment = async function (key) {
return await module.client.incr(key);
};
module.rename = async function (oldKey, newKey) {
try {
await module.client.rename(oldKey, newKey);
} catch (err) {
if (err && err.message !== 'ERR no such key') {
throw err;
}
}
module.objectCache.del([oldKey, newKey]);
};
module.type = async function (key) {
const type = await module.client.type(key);
return type !== 'none' ? type : null;
};
module.expire = async function (key, seconds) {
await module.client.EXPIRE(key, seconds);
};
module.expireAt = async function (key, timestamp) {
await module.client.EXPIREAT(key, timestamp);
};
module.pexpire = async function (key, ms) {
await module.client.PEXPIRE(key, ms);
};
module.pexpireAt = async function (key, timestamp) {
await module.client.PEXPIREAT(key, timestamp);
};
module.ttl = async function (key) {
return await module.client.TTL(key);
};
module.pttl = async function (key) {
return await module.client.PTTL(key);
};
};