Selaa lähdekoodia

fix(redis-inspector): improve scan method to handle cursor pagination and batch results

Dave 1 kuukausi sitten
vanhempi
säilyke
e9893a0010

+ 20 - 10
apps/box-mgnt-api/src/mgnt-backend/feature/redis-inspector/redis-inspector.service.ts

@@ -63,19 +63,29 @@ export class RedisInspectorService {
   }
 
   async scan(dto: ScanRedisKeysDto): Promise<RedisInspectorScanResult> {
-    const cursor = this.normalizeCursor(dto.cursor);
     const count = this.normalizePageSize(dto.pageSize);
     const matchPattern = this.buildMatchPattern(dto);
-    const [cursorNext, keys] = await this.redisService.scan(
-      cursor,
-      'MATCH',
-      matchPattern,
-      'COUNT',
-      count,
-    );
+    let cursor = this.normalizeCursor(dto.cursor);
+    const keys: string[] = [];
+
+    do {
+      const [cursorNext, batch] = await this.redisService.scan(
+        cursor,
+        'MATCH',
+        matchPattern,
+        'COUNT',
+        count,
+      );
+
+      if (batch?.length) {
+        keys.push(...batch);
+      }
+
+      cursor = cursorNext;
+    } while (cursor !== '0' && keys.length < count);
 
     if (!keys?.length) {
-      return { cursorNext, items: [] };
+      return { cursorNext: cursor, items: [] };
     }
 
     const details = await this.redisService.pipelineTypeTtl(keys);
@@ -86,7 +96,7 @@ export class RedisInspectorService {
     }));
 
     return {
-      cursorNext,
+      cursorNext: cursor,
       items,
     };
   }