Skip to content

Commit ba31447

Browse files
authored
Merge pull request #619 from clidey/claude/issue-618-20250910-0904
fix(redis): replace KEYS with SCAN for better performance
2 parents 5879592 + 8091aba commit ba31447

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

core/src/plugins/redis/redis.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (p *RedisPlugin) IsAvailable(config *engine.PluginConfig) bool {
4545

4646
func (p *RedisPlugin) GetDatabases(config *engine.PluginConfig) ([]string, error) {
4747
maxDatabases := 16
48-
availableDatabases := []string{}
48+
var availableDatabases []string
4949

5050
for i := 0; i < maxDatabases; i++ {
5151
dbConfig := *config
@@ -79,10 +79,23 @@ func (p *RedisPlugin) GetStorageUnits(config *engine.PluginConfig, schema string
7979
}
8080
defer client.Close()
8181

82-
keys, err := client.Keys(ctx, "*").Result()
83-
if err != nil {
84-
log.Logger.WithError(err).Error("Failed to retrieve Redis keys")
85-
return nil, err
82+
var keys []string
83+
var cursor uint64
84+
85+
for {
86+
var scanKeys []string
87+
scanKeys, cursor, err = client.Scan(ctx, cursor, "*", 0).Result() // count = 0 will use the redis default of 10
88+
if err != nil {
89+
log.Logger.WithError(err).Error("Failed to scan Redis keys")
90+
return nil, err
91+
}
92+
93+
keys = append(keys, scanKeys...)
94+
95+
// When cursor is 0, we've completed the full scan
96+
if cursor == 0 {
97+
break
98+
}
8699
}
87100

88101
pipe := client.Pipeline()
@@ -239,7 +252,7 @@ func (p *RedisPlugin) GetRows(
239252
log.Logger.WithError(err).WithField("storageUnit", storageUnit).Error("Failed to get Redis hash values")
240253
return nil, err
241254
}
242-
rows := [][]string{}
255+
var rows [][]string
243256
for field, value := range hashValues {
244257
if where == nil || filterRedisHash(field, value, where) {
245258
rows = append(rows, []string{field, value})
@@ -259,7 +272,7 @@ func (p *RedisPlugin) GetRows(
259272
log.Logger.WithError(err).WithField("storageUnit", storageUnit).Error("Failed to get Redis list values")
260273
return nil, err
261274
}
262-
rows := [][]string{}
275+
var rows [][]string
263276
for i, value := range listValues {
264277
if where == nil || filterRedisList(value, where) {
265278
rows = append(rows, []string{strconv.Itoa(i), value})
@@ -275,7 +288,7 @@ func (p *RedisPlugin) GetRows(
275288
log.Logger.WithError(err).WithField("storageUnit", storageUnit).Error("Failed to get Redis set values")
276289
return nil, err
277290
}
278-
rows := [][]string{}
291+
var rows [][]string
279292
for i, value := range setValues {
280293
rows = append(rows, []string{strconv.Itoa(i), value})
281294
}
@@ -290,7 +303,7 @@ func (p *RedisPlugin) GetRows(
290303
log.Logger.WithError(err).WithField("storageUnit", storageUnit).Error("Failed to get Redis zset values")
291304
return nil, err
292305
}
293-
rows := [][]string{}
306+
var rows [][]string
294307
for i, member := range zsetValues {
295308
rows = append(rows, []string{strconv.Itoa(i), member.Member.(string), fmt.Sprintf("%.2f", member.Score)})
296309
}

0 commit comments

Comments
 (0)