From 171f2a183a99cdfa334319df425bdb5c573a1d04 Mon Sep 17 00:00:00 2001 From: Noa Milshtein Date: Thu, 21 Dec 2023 15:15:24 +0200 Subject: [PATCH 1/3] add cursor and count scan params --- src/redis.ts | 5 ++--- src/types/RedisStore.interface.ts | 2 +- test/redis.test.ts | 15 ++++++++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/redis.ts b/src/redis.ts index 3935820..ea4a7fb 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): 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..870c973 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, coursor? :number, count?: number): Promise; } diff --git a/test/redis.test.ts b/test/redis.test.ts index eb739e3..b511078 100644 --- a/test/redis.test.ts +++ b/test/redis.test.ts @@ -91,7 +91,20 @@ describe('Redis Store', () => { await redisClient.set(key2, value, ttl); await redisClient.set(key3, value, ttl); - const res = await redisClient.scan('ttl:a:*'); + const res = await redisClient.scan('ttl:a:*', 0, 10); 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); + }); }); From 651216a9021a2a17ad6d57f0419d6b98202e16c2 Mon Sep 17 00:00:00 2001 From: Noa Milshtein Date: Thu, 21 Dec 2023 15:34:10 +0200 Subject: [PATCH 2/3] typo --- src/types/RedisStore.interface.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/RedisStore.interface.ts b/src/types/RedisStore.interface.ts index 870c973..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, coursor? :number, count?: number): Promise; + scan(pattern: string, cursor? :number, count?: number): Promise; } From b8a12905ae5ee790cadc902d82b9213a6701a06e Mon Sep 17 00:00:00 2001 From: Noa Milshtein Date: Thu, 21 Dec 2023 15:45:53 +0200 Subject: [PATCH 3/3] add default value to count --- src/redis.ts | 2 +- test/redis.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/redis.ts b/src/redis.ts index ea4a7fb..bb8e7bf 100644 --- a/src/redis.ts +++ b/src/redis.ts @@ -106,7 +106,7 @@ class buildRedisStoreWithConfig implements RedisStore { return await this.redisCache.ttl(key); }; - public async scan(pattern: string, cursor: number = 0, count?: number): Promise { + public async scan(pattern: string, cursor: number = 0, count: number = 10): Promise { return await this.redisCache.scan(cursor, { MATCH: pattern, COUNT: count }); } diff --git a/test/redis.test.ts b/test/redis.test.ts index b511078..8f6b94e 100644 --- a/test/redis.test.ts +++ b/test/redis.test.ts @@ -91,7 +91,7 @@ describe('Redis Store', () => { await redisClient.set(key2, value, ttl); await redisClient.set(key3, value, ttl); - const res = await redisClient.scan('ttl:a:*', 0, 10); + const res = await redisClient.scan('ttl:a:*'); expect(res.keys).toEqual([key1, key3]); const firstScanWithCount = await redisClient.scan('ttl:a:*', 0, 1);