Mscan
Bun

method

RedisClient.scan

cursor: string | number
): Promise<[string, string[]]>;

Incrementally iterate the keyspace

The SCAN command is used to incrementally iterate over a collection of elements. SCAN iterates the set of keys in the currently selected Redis database.

SCAN is a cursor based iterator. This means that at every call of the command, the server returns an updated cursor that the user needs to use as the cursor argument in the next call.

An iteration starts when the cursor is set to "0", and terminates when the cursor returned by the server is "0".

@param cursor

The cursor value (use "0" to start a new iteration)

@returns

Promise that resolves with a tuple [cursor, keys[]] where cursor is the next cursor to use (or "0" if iteration is complete) and keys is an array of matching keys

// Basic scan - iterate all keys
let cursor = "0";
const allKeys: string[] = [];
do {
  const [nextCursor, keys] = await redis.scan(cursor);
  allKeys.push(...keys);
  cursor = nextCursor;
} while (cursor !== "0");
cursor: string | number,
match: 'MATCH',
pattern: string
): Promise<[string, string[]]>;

Incrementally iterate the keyspace with a pattern match

@param cursor

The cursor value (use "0" to start a new iteration)

@param match

The "MATCH" keyword

@param pattern

The pattern to match (supports glob-style patterns like "user:*")

@returns

Promise that resolves with a tuple [cursor, keys[]]

cursor: string | number,
count: 'COUNT',
hint: number
): Promise<[string, string[]]>;

Incrementally iterate the keyspace with a count hint

@param cursor

The cursor value (use "0" to start a new iteration)

@param count

The "COUNT" keyword

@param hint

The number of elements to return per call (hint only, not exact)

@returns

Promise that resolves with a tuple [cursor, keys[]]

cursor: string | number,
match: 'MATCH',
pattern: string,
count: 'COUNT',
hint: number
): Promise<[string, string[]]>;

Incrementally iterate the keyspace with pattern match and count hint

@param cursor

The cursor value (use "0" to start a new iteration)

@param match

The "MATCH" keyword

@param pattern

The pattern to match

@param count

The "COUNT" keyword

@param hint

The number of elements to return per call

@returns

Promise that resolves with a tuple [cursor, keys[]]

cursor: string | number,
...options: string | number[]
): Promise<[string, string[]]>;

Incrementally iterate the keyspace with options

@param cursor

The cursor value

@param options

Additional SCAN options (MATCH pattern, COUNT hint, etc.)

@returns

Promise that resolves with a tuple [cursor, keys[]]