Skip to content

Commit

Permalink
add cursor and count params to scan
Browse files Browse the repository at this point in the history
  • Loading branch information
noamilshtein committed Dec 21, 2023
1 parent 209366b commit 39ed927
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ class buildRedisStoreWithConfig implements RedisStore {
return await this.redisCache.ttl(key);
};

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

public getClient(): RedisClientType<RedisDefaultModules & RedisModules, RedisFunctions, RedisScripts> {
Expand Down
2 changes: 1 addition & 1 deletion src/types/RedisStore.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export interface RedisStore extends Store {

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

scan(pattern: string): Promise<ScanReply>;
scan(pattern: string, cursor? :number, count?: number): Promise<ScanReply>;
}
13 changes: 13 additions & 0 deletions test/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

});
});

0 comments on commit 39ed927

Please sign in to comment.