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()
|
||||||
if async {
|
for _, k2 := range ks {
|
||||||
fn = cli.Unlink
|
if async {
|
||||||
} else {
|
cli.Unlink(ctx, k2)
|
||||||
fn = cli.Del
|
} else {
|
||||||
}
|
cli.Del(ctx, k2)
|
||||||
for iter.Next(ctx) {
|
}
|
||||||
subKey := iter.Val()
|
|
||||||
if err = fn(ctx, subKey).Err(); err != nil {
|
|
||||||
return err
|
|
||||||
} else {
|
|
||||||
mutex.Lock()
|
|
||||||
deletedKeys = append(deletedKeys, subKey)
|
|
||||||
mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
pipe.Exec(ctx)
|
||||||
|
|
||||||
|
mutex.Lock()
|
||||||
|
deletedKeys = append(deletedKeys, ks...)
|
||||||
|
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