Skip to content

Commit a4d6e1f

Browse files
authored
add cursor and count params to scan (#12)
1 parent 209366b commit a4d6e1f

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/redis.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,8 @@ class buildRedisStoreWithConfig implements RedisStore {
106106
return await this.redisCache.ttl(key);
107107
};
108108

109-
public async scan(pattern: string): Promise<ScanReply> {
110-
let cursor = 0;
111-
return await this.redisCache.scan(cursor, {MATCH: pattern});
109+
public async scan(pattern: string, cursor: number = 0, count: number = 10): Promise<ScanReply> {
110+
return await this.redisCache.scan(cursor, { MATCH: pattern, COUNT: count });
112111
}
113112

114113
public getClient(): RedisClientType<RedisDefaultModules & RedisModules, RedisFunctions, RedisScripts> {

src/types/RedisStore.interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export interface RedisStore extends Store {
77

88
getClient(): RedisClientType<RedisDefaultModules & RedisModules, RedisFunctions, RedisScripts>;
99

10-
scan(pattern: string): Promise<ScanReply>;
10+
scan(pattern: string, cursor? :number, count?: number): Promise<ScanReply>;
1111
}

test/redis.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,18 @@ describe('Redis Store', () => {
9393

9494
const res = await redisClient.scan('ttl:a:*');
9595
expect(res.keys).toEqual([key1, key3]);
96+
97+
const firstScanWithCount = await redisClient.scan('ttl:a:*', 0, 1);
98+
expect(firstScanWithCount.keys).toEqual([key1]);
99+
expect(firstScanWithCount.cursor).not.equal(0);
100+
101+
const secondScanWithCount = await redisClient.scan('ttl:a:*', firstScanWithCount.cursor, 1);
102+
expect(secondScanWithCount.keys).toEqual([key3]);
103+
expect(secondScanWithCount.cursor).equal(3);
104+
105+
const thirdScanWithCount = await redisClient.scan('ttl:a:*', secondScanWithCount.cursor, 1);
106+
expect(thirdScanWithCount.keys).toEqual([]);
107+
expect(thirdScanWithCount.cursor).equal(0);
108+
96109
});
97110
});

0 commit comments

Comments
 (0)