gopkg/middleware/etcd/delete.go

55 lines
1.3 KiB
Go
Raw Permalink Normal View History

2021-11-24 14:18:31 +08:00
// Package etcd...
//
// Description : 删除相关操作
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-24 12:42 下午
package etcd
import (
"context"
"errors"
"time"
"go.etcd.io/etcd/clientv3"
)
// DeleteWithOption ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:02 下午 2021/11/24
func DeleteWithOption(ctx context.Context, key string, timeout time.Duration, optionList ...clientv3.OpOption) (*clientv3.DeleteResponse, error) {
if nil == ctx {
ctx = context.Background()
}
if timeout <= 0 {
timeout = DefaultDeleteTimeout
}
var (
cancelFunc context.CancelFunc
deleteResponse *clientv3.DeleteResponse
err error
)
ctx, cancelFunc = context.WithTimeout(ctx, timeout)
defer cancelFunc()
if deleteResponse, err = Client.Delete(ctx, key, optionList...); nil != err {
return nil, errors.New("delete key fail : " + err.Error())
}
return deleteResponse, nil
}
// DeleteWithKeyPrefix 基于 key 前缀, 删除key
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2:12 下午 2021/11/24
func DeleteWithKeyPrefix(ctx context.Context, keyPrefix string, timeout time.Duration) (*clientv3.DeleteResponse, error) {
return DeleteWithOption(ctx, keyPrefix, timeout, clientv3.WithPrefix())
}