From 096c33e11c72ab05f0dc2634aefd98da0e164f95 Mon Sep 17 00:00:00 2001 From: Alfex4936 Date: Sat, 20 Apr 2024 12:17:29 +0900 Subject: [PATCH] [Backend] Fix Redis cursor --- backend/services/redis_services.go | 32 ++++++++++-------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/backend/services/redis_services.go b/backend/services/redis_services.go index bfcc6702..563ea821 100644 --- a/backend/services/redis_services.go +++ b/backend/services/redis_services.go @@ -3,7 +3,6 @@ package services import ( "context" "errors" - "strconv" "time" "github.com/goccy/go-json" @@ -105,35 +104,24 @@ func ResetAllCache(pattern string) error { scanCmd := RedisStore.B().Scan().Cursor(cursor).Match(pattern).Count(10).Build() // Execute the SCAN command to find keys matching the pattern - resp, err := RedisStore.Do(ctx, scanCmd).ToArray() + scanEntry, err := RedisStore.Do(ctx, scanCmd).AsScanEntry() if err != nil { return err } - // First element is the new cursor, subsequent elements are the keys - if len(resp) > 0 { - newCursorStr := resp[0].String() - newCursor, err := strconv.ParseUint(newCursorStr, 10, 64) - if err != nil { - return err // handle parsing error - } - cursor = newCursor - - keys := make([]string, 0, len(resp)-1) - for _, msg := range resp[1:] { - keys = append(keys, msg.String()) - } + // Use the ScanEntry for cursor and keys directly + cursor = scanEntry.Cursor + keys := scanEntry.Elements - // Delete keys using individual DEL commands - for _, key := range keys { - delCmd := RedisStore.B().Del().Key(key).Build() - if err := RedisStore.Do(ctx, delCmd).Error(); err != nil { - return err - } + // Delete keys using individual DEL commands + for _, key := range keys { + delCmd := RedisStore.B().Del().Key(key).Build() + if err := RedisStore.Do(ctx, delCmd).Error(); err != nil { + return err } } - // If the cursor returned by SCAN is 0, we've iterated through all the keys + // If the cursor returned by SCAN is 0, iterated through all the keys if cursor == 0 { break }