perf: use pipeline for batch delete keys
This commit is contained in:
parent
72144bc996
commit
e2a371ed14
|
@ -1343,22 +1343,36 @@ func (c *connectionService) DeleteKey(connName string, db int, k any, async bool
|
||||||
// delete by prefix
|
// delete by prefix
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
del := func(ctx context.Context, cli redis.UniversalClient) error {
|
del := func(ctx context.Context, cli redis.UniversalClient) error {
|
||||||
iter := cli.Scan(ctx, 0, key, 10000).Iterator()
|
handleDel := func(ks []string) error {
|
||||||
var fn func(c context.Context, ks ...string) *redis.IntCmd
|
pipe := cli.Pipeline()
|
||||||
|
for _, k2 := range ks {
|
||||||
if async {
|
if async {
|
||||||
fn = cli.Unlink
|
cli.Unlink(ctx, k2)
|
||||||
} else {
|
} else {
|
||||||
fn = cli.Del
|
cli.Del(ctx, k2)
|
||||||
}
|
}
|
||||||
for iter.Next(ctx) {
|
}
|
||||||
subKey := iter.Val()
|
pipe.Exec(ctx)
|
||||||
if err = fn(ctx, subKey).Err(); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
deletedKeys = append(deletedKeys, subKey)
|
deletedKeys = append(deletedKeys, ks...)
|
||||||
mutex.Unlock()
|
mutex.Unlock()
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
iter := cli.Scan(ctx, 0, key, 10000).Iterator()
|
||||||
|
resultKeys := make([]string, 0, 100)
|
||||||
|
for iter.Next(ctx) {
|
||||||
|
resultKeys = append(resultKeys, iter.Val())
|
||||||
|
if len(resultKeys) >= 3 {
|
||||||
|
handleDel(resultKeys)
|
||||||
|
resultKeys = resultKeys[:0:cap(resultKeys)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(resultKeys) > 0 {
|
||||||
|
handleDel(resultKeys)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue