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

Ensure complete retrieval of keys when using Redis SCAN command. #5

Open
ofluffydev opened this issue Sep 25, 2024 · 0 comments
Open

Comments

@ofluffydev
Copy link
Contributor

          **Ensure complete retrieval of keys when using Redis SCAN command.**

The current usage of redis.scan may not retrieve all matching keys, as SCAN is a cursor-based iterator that requires looping until the cursor returns zero. The scan command fetches a subset of keys per call and provides a cursor for the next set.

Consider modifying the code to iterate over the cursor to collect all matching keys:

let cursor = 0;
const hashKeys: string[] = [];

do {
  const reply = await redis.scan(cursor, { MATCH: 'hash:*' });
  cursor = reply.cursor;
  hashKeys.push(...reply.keys);
} while (cursor !== 0);

const hashes = hashKeys.map((hash) => hash.substring(5));

This ensures that all matching keys are retrieved. Alternatively, if the number of keys is expected to be small, you might consider organizing your data differently in Redis to allow more efficient retrieval.

Originally posted by @coderabbitai[bot] in #1 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant