UNPKG

redis-smq

Version:

A high-performance, reliable, and scalable message queue for Node.js.

69 lines 3.39 kB
import { async } from 'redis-smq-common'; import { BrowserStorageAbstract } from './browser-storage-abstract.js'; import { withSharedPoolConnection } from '../../../common/redis/redis-connection-pool/with-shared-pool-connection.js'; export class BrowserStorageSortedSet extends BrowserStorageAbstract { count(redisKey, cb) { this.logger.debug(`Counting items in ${redisKey}`); withSharedPoolConnection((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}]`); withSharedPoolConnection((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`); withSharedPoolConnection((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=browser-storage-sorted-set.js.map