Skip to content

Commit 847e60b

Browse files
claude[bot]hkdeman
andcommitted
fix(redis): replace KEYS with SCAN for better performance
Replace Redis KEYS command with SCAN to improve performance on large datasets. The KEYS command can block the Redis server when dealing with many keys, while SCAN provides cursor-based iteration that is non-blocking. Fixes #618 Co-authored-by: H <[email protected]>
1 parent e6af87c commit 847e60b

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

core/src/plugins/redis/redis.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,24 @@ func (p *RedisPlugin) GetStorageUnits(config *engine.PluginConfig, schema string
7777
}
7878
defer client.Close()
7979

80-
keys, err := client.Keys(ctx, "*").Result()
81-
if err != nil {
82-
log.Logger.WithError(err).Error("Failed to retrieve Redis keys")
83-
return nil, err
80+
// Use SCAN instead of KEYS for better performance
81+
var keys []string
82+
var cursor uint64
83+
84+
for {
85+
var scanKeys []string
86+
scanKeys, cursor, err = client.Scan(ctx, cursor, "*", 0).Result()
87+
if err != nil {
88+
log.Logger.WithError(err).Error("Failed to scan Redis keys")
89+
return nil, err
90+
}
91+
92+
keys = append(keys, scanKeys...)
93+
94+
// When cursor is 0, we've completed the full scan
95+
if cursor == 0 {
96+
break
97+
}
8498
}
8599

86100
pipe := client.Pipeline()

0 commit comments

Comments
 (0)