UNPKG

@hotmeshio/hotmesh

Version:

Permanent-Memory Workflows & AI Agents

200 lines (199 loc) 7.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisStoreService = void 0; const enums_1 = require("../../../../modules/enums"); const _base_1 = require("./_base"); class RedisStoreService extends _base_1.RedisStoreBase { constructor(storeClient) { super(storeClient); this.commands = { get: 'GET', set: 'SET', setnx: 'SETNX', del: 'DEL', expire: 'EXPIRE', hscan: 'HSCAN', hset: 'HSET', hsetnx: 'HSETNX', hincrby: 'HINCRBY', hdel: 'HDEL', hget: 'HGET', hmget: 'HMGET', hgetall: 'HGETALL', hincrbyfloat: 'HINCRBYFLOAT', zrange: 'ZRANGE', zrangebyscore_withscores: 'ZRANGEBYSCORE_WITHSCORES', zrangebyscore: 'ZRANGEBYSCORE', zrem: 'ZREM', zadd: 'ZADD', lmove: 'LMOVE', lrange: 'LRANGE', lpop: 'LPOP', rename: 'RENAME', rpush: 'RPUSH', scan: 'SCAN', xack: 'XACK', xdel: 'XDEL', xlen: 'XLEN', }; } /** * When in cluster mode, the transact wrapper only * sends commands to the same node/shard if they share a key. * All other commands are sent simultaneouslyusing Promise.all * and are then collated */ transact() { const my = this; if (enums_1.HMSH_IS_CLUSTER) { const commands = []; const addCommand = (command, args) => { commands.push({ command: command.toUpperCase(), args }); return multiInstance; }; const multiInstance = { sendCommand(command, ...args) { return my.storeClient.sendCommand([command, ...args]); }, async exec() { if (commands.length === 0) return []; const sameKey = commands.every((cmd) => { return cmd.args[0] === commands[0].args[0]; }); if (sameKey) { const multi = my.storeClient.multi(); commands.forEach((cmd) => { if (cmd.command === 'ZADD') { return multi.ZADD(cmd.args[0], cmd.args[1], cmd.args[2]); } return multi[cmd.command](...cmd.args); }); const results = await multi.exec(); return results.map((item) => item); } else { return Promise.all(commands.map((cmd) => { if (cmd.command === 'ZADD') { return my.storeClient.ZADD(cmd.args[0], cmd.args[1], cmd.args[2]); } return my.storeClient[cmd.command](...cmd.args); })); } }, XADD(key, id, fields, message) { return addCommand('XADD', [key, id, fields, message]); }, XACK(key, group, id) { return addCommand('XACK', [key, group, id]); }, XDEL(key, id) { return addCommand('XDEL', [key, id]); }, XLEN(key) { return addCommand('XLEN', [key]); }, XCLAIM(key, group, consumer, minIdleTime, id, ...args) { return addCommand('XCLAIM', [ key, group, consumer, minIdleTime, id, ...args, ]); }, XPENDING(key, group, start, end, count, consumer) { return addCommand('XPENDING', [ key, group, start, end, count, consumer, ]); }, DEL: function (key) { return addCommand('DEL', [key]); }, EXPIRE: function (key, seconds) { return addCommand('EXPIRE', [key, seconds]); }, HDEL(key, itemId) { return addCommand('HDEL', [key, itemId]); }, HGET(key, itemId) { return addCommand('HGET', [key, itemId]); }, HGETALL(key) { return addCommand('HGETALL', [key]); }, HINCRBYFLOAT(key, itemId, value) { return addCommand('HINCRBYFLOAT', [key, itemId, value]); }, HMGET(key, itemIds) { return addCommand('HMGET', [key, itemIds]); }, HSET(key, values) { return addCommand('HSET', [key, values]); }, LRANGE(key, start, end) { return addCommand('LRANGE', [key, start, end]); }, RPUSH(key, items) { return addCommand('RPUSH', [key, items]); }, ZADD(key, args, opts) { return addCommand('ZADD', [key, args, opts]); }, XGROUP(command, key, groupName, id, mkStream) { return addCommand('XGROUP', [command, key, groupName, id, mkStream]); }, EXISTS: function (key) { throw new Error('Function not implemented.'); }, HMPUSH: function (key, values) { throw new Error('Function not implemented.'); }, LPUSH: function (key, items) { throw new Error('Function not implemented.'); }, SET: function (key, value) { throw new Error('Function not implemented.'); }, ZRANGE_WITHSCORES: function (key, start, end) { throw new Error('Function not implemented.'); }, ZRANK: function (key, member) { throw new Error('Function not implemented.'); }, ZSCORE: function (key, value) { throw new Error('Function not implemented.'); }, }; return multiInstance; } return this.storeClient.multi(); } async exec(...args) { return await this.storeClient.sendCommand(args); } async setnxex(key, value, expireSeconds) { const status = await this.storeClient[this.commands.set](key, value, { NX: true, EX: expireSeconds }); return this.isSuccessful(status); } async zAdd(key, score, value, redisMulti) { return await (redisMulti || this.storeClient)[this.commands.zadd](key, { score: score, value: value.toString(), }); } async zRangeByScore(key, score, value) { const result = await this.storeClient[this.commands.zrangebyscore](key, score, value); if (result?.length > 0) { return result[0]; } return null; } } exports.RedisStoreService = RedisStoreService;