perf: multiple kv keys

This commit is contained in:
KernelDeimos
2024-12-16 15:38:30 -05:00
parent 29d4eefbae
commit 5a68691290
2 changed files with 24 additions and 1 deletions
+23
View File
@@ -32,6 +32,29 @@ class DBKVService extends BaseService {
app = await get_app({ uid: app_uid });
}
if ( Array.isArray(key) ) {
const keys = key;
const key_hashes = keys.map(key => this.modules.murmurhash.v3(key));
const rows = app ? await this.db.read(
`SELECT kkey, value FROM kv WHERE user_id=? AND app=? AND kkey_hash IN (?)`,
[ user.id, app.uid, key_hashes ]
) : await this.db.read(
`SELECT kkey, value FROM kv WHERE user_id=? AND (app IS NULL OR app = 'global') AND kkey_hash IN (?)`,
[ user.id, key_hashes ]
);
const kv = {};
rows.forEach(row => {
row.value = this.db.case({
mysql: () => row.value,
otherwise: () => JSON.parse(row.value ?? 'null'),
})();
kv[row.kkey] = row.value;
});
return keys.map(key => kv[key]);
}
const key_hash = this.modules.murmurhash.v3(key);
const kv = app ? await this.db.read(
`SELECT * FROM kv WHERE user_id=? AND app=? AND kkey_hash=? LIMIT 1`,
@@ -111,7 +111,7 @@ module.exports = {
get: {
description: 'Get a value by key.',
parameters: {
key: { type: 'string', required: true },
key: { type: 'json', required: true },
app_uid: { type: 'string', optional: true },
},
result: { type: 'json' },