Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

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
Collaborator

          **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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant