UNPKG

redis-smq

Version:

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

72 lines 3.46 kB
import { async, withRedisClient } from 'redis-smq-common'; import { QueueMessagesStorage } from './queue-messages-storage.js'; export class QueueMessagesStorageSortedSet extends QueueMessagesStorage { constructor(redisClient) { super(redisClient); this.logger.debug(`${this.constructor.name} initialized`); } count(redisKey, cb) { this.logger.debug(`Counting items in ${redisKey}`); withRedisClient(this.redisClient, (client, cb) => { this.logger.debug(`Executing ZCARD on ${redisKey}`); client.zcard(redisKey, (err, count) => { if (err) { this.logger.error(`ZCARD failed for ${redisKey}: ${err.message}`); cb(err); } else { this.logger.debug(`${redisKey} contains ${count} items`); cb(null, count); } }); }, cb); } fetchItems(redisKey, pageParams, cb) { const { offsetStart, offsetEnd } = pageParams; this.logger.debug(`Fetching items from ${redisKey} range [${offsetStart}:${offsetEnd}]`); withRedisClient(this.redisClient, (client, cb) => { this.logger.debug(`Executing ZRANGE on ${redisKey} from ${offsetStart} to ${offsetEnd}`); client.zrange(redisKey, offsetStart, offsetEnd, (err, items) => { if (err) { this.logger.error(`ZRANGE failed for ${redisKey}: ${err.message}`); cb(err); } else { const itemCount = items ? items.length : 0; if (itemCount === 0) { this.logger.debug(`No items found in ${redisKey} range [${offsetStart}:${offsetEnd}]`); } else { this.logger.debug(`Retrieved ${itemCount} items from ${redisKey}`); } cb(null, items || []); } }); }, cb); } fetchAllItems(redisKey, cb) { this.logger.debug(`Fetching all items from ${redisKey} using ZSCAN`); withRedisClient(this.redisClient, (client, cb) => { const allItems = []; let scanCount = 0; const scanRecursive = (cursor) => { scanCount++; this.logger.debug(`ZSCAN iteration ${scanCount} on ${redisKey}, cursor: ${cursor}`); async.withCallback((cb) => client.zscan(redisKey, cursor, { COUNT: 100 }, cb), (result, cb) => { const batchSize = result.items.length; allItems.push(...result.items); this.logger.debug(`ZSCAN batch #${scanCount} retrieved ${batchSize} items, total collected: ${allItems.length}`); if (result.cursor === '0') { this.logger.debug(`ZSCAN complete, collected ${allItems.length} items in ${scanCount} iterations`); return cb(null, allItems); } this.logger.debug(`ZSCAN continuing with cursor=${result.cursor}`); setImmediate(() => scanRecursive(result.cursor)); }, cb); }; this.logger.debug(`Starting ZSCAN with initial cursor=0`); scanRecursive('0'); }, cb); } } //# sourceMappingURL=queue-messages-storage-sorted-set.js.map