@@ -102,6 +102,10 @@ async def scan_keys(pattern: str = "*", count: int = 100, cursor: int = 0) -> di
102
102
"""
103
103
Scan keys in the Redis database using the SCAN command (non-blocking, production-safe).
104
104
105
+ ⚠️ IMPORTANT: This returns PARTIAL results from one iteration. Use scan_all_keys()
106
+ to get ALL matching keys, or call this function multiple times with the returned cursor
107
+ until cursor becomes 0.
108
+
105
109
The SCAN command iterates through the keyspace in small chunks, making it safe to use
106
110
on large databases without blocking other operations.
107
111
@@ -111,13 +115,20 @@ async def scan_keys(pattern: str = "*", count: int = 100, cursor: int = 0) -> di
111
115
count: Hint for the number of keys to return per iteration (default 100).
112
116
Redis may return more or fewer keys than this hint.
113
117
cursor: The cursor position to start scanning from (0 to start from beginning).
118
+ To continue scanning, use the cursor value returned from previous call.
114
119
115
120
Returns:
116
121
A dictionary containing:
117
122
- 'cursor': Next cursor position (0 means scan is complete)
118
- - 'keys': List of keys found in this iteration
123
+ - 'keys': List of keys found in this iteration (PARTIAL RESULTS)
119
124
- 'total_scanned': Number of keys returned in this batch
125
+ - 'scan_complete': Boolean indicating if scan is finished
120
126
Or an error message if something goes wrong.
127
+
128
+ Example usage:
129
+ First call: scan_keys("user:*") -> returns cursor=1234, keys=[...], scan_complete=False
130
+ Next call: scan_keys("user:*", cursor=1234) -> continues from where it left off
131
+ Final call: returns cursor=0, scan_complete=True when done
121
132
"""
122
133
try :
123
134
r = RedisConnectionManager .get_connection ()
@@ -143,6 +154,9 @@ async def scan_all_keys(pattern: str = "*", batch_size: int = 100) -> list:
143
154
144
155
This function automatically handles the SCAN cursor iteration to collect all matching keys.
145
156
It's safer than KEYS * for large databases but will still collect all results in memory.
157
+
158
+ ⚠️ WARNING: With very large datasets (millions of keys), this may consume significant memory.
159
+ For large-scale operations, consider using scan_keys() with manual iteration instead.
146
160
147
161
Args:
148
162
pattern: Pattern to match keys against (default is "*" for all keys).
0 commit comments