UNPKG

redis-smq

Version:

A simple high-performance Redis message queue for Node.js.

71 lines 2.88 kB
import { CallbackEmptyReplyError } from 'redis-smq-common'; import { redisKeys } from '../../../common/redis-keys/redis-keys.js'; import { QueueMessagesStorage } from './queue-messages-storage.js'; export class QueueMessagesStorageList extends QueueMessagesStorage { count(queue, redisKey, cb) { const keys = redisKeys.getQueueKeys(queue.queueParams, queue.groupId); this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else { client.llen(keys[redisKey], cb); } }); } fetchItems(queue, redisKey, offset, limit, cb) { const keys = redisKeys.getQueueKeys(queue.queueParams, queue.groupId); this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else { client.lrange(keys[redisKey], offset, offset + limit - 1, (err, items) => { if (err) cb(err); else cb(null, items || []); }); } }); } fetchAllItems(queue, redisKey, cb) { const chunkSize = 100; this.redisClient.getSetInstance((err, client) => { if (err) cb(err); else if (!client) cb(new CallbackEmptyReplyError()); else { const keys = redisKeys.getQueueKeys(queue.queueParams, queue.groupId); const key = keys[redisKey]; client.llen(key, (err, reply) => { if (err) return cb(err); const totalLength = Number(reply); if (totalLength === 0) return cb(null, []); const allItems = []; const processChunk = (offset) => { if (offset >= totalLength) { return cb(null, allItems); } const end = Math.min(offset + chunkSize - 1, totalLength - 1); client.lrange(key, offset, end, (err, items) => { if (err) return cb(err); if (!items || items.length === 0) return cb(null, allItems); allItems.push(...items); setImmediate(() => processChunk(offset + chunkSize)); }); }; processChunk(0); }); } }); } } //# sourceMappingURL=queue-message-storage-list.js.map