Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(be): add cursor and count scan params #11

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

});
});