gopkg/middleware/etcd/string.go

68 lines
1.3 KiB
Go
Raw Normal View History

2021-11-23 12:31:45 +08:00
// Package etcd...
//
// Description : etcd...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-23 12:09 下午
package etcd
import (
"context"
"time"
"github.com/coreos/etcd/mvcc/mvccpb"
"go.etcd.io/etcd/clientv3"
)
// Put put数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:09 下午 2021/11/23
func Put(ctx context.Context, key string, val string, operateTimeout time.Duration) error {
if operateTimeout <= 0 {
operateTimeout = DefaultPutTimeout
}
var (
cancel context.CancelFunc
err error
)
if nil == ctx {
ctx = context.Background()
}
ctx, cancel = context.WithTimeout(ctx, operateTimeout)
_, err = Client.Put(ctx, key, val)
cancel()
return err
}
// Get 读取数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:09 下午 2021/11/23
func Get(ctx context.Context, key string, operateTimeout time.Duration) (
[]*mvccpb.KeyValue,
error,
) {
if operateTimeout <= 0 {
operateTimeout = DefaultGetTimeout
}
var (
cancel context.CancelFunc
err error
result *clientv3.GetResponse
)
if nil == ctx {
ctx = context.Background()
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
result, err = Client.Get(ctx, key)
cancel()
if err != nil {
return nil, err
}
return result.Kvs, nil
}