From a4d6e1fb412ee67123b8e7a9141a2af47ddc6bd8 Mon Sep 17 00:00:00 2001 From: noamilshtein <116799914+noamilshtein@users.noreply.github.com> Date: Thu, 21 Dec 2023 16:39:42 +0200 Subject: [PATCH] add cursor and count params to scan (#12) --- src/redis.ts | 5 ++--- src/types/RedisStore.interface.ts | 2 +- test/redis.test.ts | 13 +++++++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/redis.ts b/src/redis.ts index 3935820..bb8e7bf 100644 --- a/src/redis.ts +++ b/src/redis.ts @@ -106,9 +106,8 @@ class buildRedisStoreWithConfig implements RedisStore { return await this.redisCache.ttl(key); }; - public async scan(pattern: string): Promise { - let cursor = 0; - return await this.redisCache.scan(cursor, {MATCH: pattern}); + public async scan(pattern: string, cursor: number = 0, count: number = 10): Promise { + return await this.redisCache.scan(cursor, { MATCH: pattern, COUNT: count }); } public getClient(): RedisClientType { diff --git a/src/types/RedisStore.interface.ts b/src/types/RedisStore.interface.ts index 56fa094..c7bb025 100644 --- a/src/types/RedisStore.interface.ts +++ b/src/types/RedisStore.interface.ts @@ -7,5 +7,5 @@ export interface RedisStore extends Store { getClient(): RedisClientType; - scan(pattern: string): Promise; + scan(pattern: string, cursor? :number, count?: number): Promise; } diff --git a/test/redis.test.ts b/test/redis.test.ts index eb739e3..8f6b94e 100644 --- a/test/redis.test.ts +++ b/test/redis.test.ts @@ -93,5 +93,18 @@ describe('Redis Store', () => { const res = await redisClient.scan('ttl:a:*'); expect(res.keys).toEqual([key1, key3]); + + const firstScanWithCount = await redisClient.scan('ttl:a:*', 0, 1); + expect(firstScanWithCount.keys).toEqual([key1]); + expect(firstScanWithCount.cursor).not.equal(0); + + const secondScanWithCount = await redisClient.scan('ttl:a:*', firstScanWithCount.cursor, 1); + expect(secondScanWithCount.keys).toEqual([key3]); + expect(secondScanWithCount.cursor).equal(3); + + const thirdScanWithCount = await redisClient.scan('ttl:a:*', secondScanWithCount.cursor, 1); + expect(thirdScanWithCount.keys).toEqual([]); + expect(thirdScanWithCount.cursor).equal(0); + }); });