feature/etcd #6
2
go.mod
2
go.mod
@ -8,6 +8,7 @@ replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
|
||||
|
||||
require (
|
||||
github.com/Shopify/sarama v1.30.0
|
||||
github.com/coreos/etcd v3.3.27+incompatible
|
||||
github.com/ddliu/go-httpclient v0.6.9
|
||||
github.com/gin-gonic/gin v1.7.5
|
||||
github.com/go-redis/redis/v8 v8.11.4
|
||||
@ -32,7 +33,6 @@ require (
|
||||
github.com/StackExchange/wmi v1.2.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/coreos/bbolt v1.3.4 // indirect
|
||||
github.com/coreos/etcd v3.3.27+incompatible // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
|
||||
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
|
||||
|
@ -37,12 +37,12 @@ func Put(ctx context.Context, key string, val string, operateTimeout time.Durati
|
||||
return err
|
||||
}
|
||||
|
||||
// Get 读取数据
|
||||
// GetWithOption 使用各种option选项读取数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:09 下午 2021/11/23
|
||||
func Get(ctx context.Context, key string, operateTimeout time.Duration) (
|
||||
// Date : 11:17 上午 2021/11/24
|
||||
func GetWithOption(ctx context.Context, key string, operateTimeout time.Duration, optionList ...clientv3.OpOption) (
|
||||
[]*mvccpb.KeyValue,
|
||||
error,
|
||||
) {
|
||||
@ -57,11 +57,45 @@ func Get(ctx context.Context, key string, operateTimeout time.Duration) (
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
|
||||
result, err = Client.Get(ctx, key)
|
||||
cancel()
|
||||
if err != nil {
|
||||
ctx, cancel = context.WithTimeout(context.Background(), operateTimeout)
|
||||
defer cancel()
|
||||
if result, err = Client.Get(ctx, key, optionList...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.Kvs, nil
|
||||
}
|
||||
|
||||
// Get 读取数据,按照key精确匹配
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:09 下午 2021/11/23
|
||||
func Get(ctx context.Context, key string, operateTimeout time.Duration) (
|
||||
[]*mvccpb.KeyValue,
|
||||
error,
|
||||
) {
|
||||
return GetWithOption(ctx, key, operateTimeout)
|
||||
}
|
||||
|
||||
// GetWithPrefix 使用key前缀查找
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:58 上午 2021/11/24
|
||||
func GetWithPrefix(ctx context.Context, keyPrefix string, operateTimeout time.Duration) (
|
||||
map[string]*mvccpb.KeyValue,
|
||||
error,
|
||||
) {
|
||||
var (
|
||||
result []*mvccpb.KeyValue
|
||||
err error
|
||||
)
|
||||
if result, err = GetWithOption(ctx, keyPrefix, operateTimeout, clientv3.WithPrefix()); nil != err {
|
||||
return make(map[string]*mvccpb.KeyValue), err
|
||||
}
|
||||
formatResult := make(map[string]*mvccpb.KeyValue)
|
||||
for _, item := range result {
|
||||
formatResult[string(item.Key)] = item
|
||||
}
|
||||
return formatResult, nil
|
||||
}
|
||||
|
@ -12,6 +12,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/gopkg/util"
|
||||
|
||||
"go.etcd.io/etcd/clientv3"
|
||||
)
|
||||
|
||||
@ -42,6 +44,19 @@ func TestGet(t *testing.T) {
|
||||
fmt.Println(Get(nil, "name", 0))
|
||||
}
|
||||
|
||||
// TestGetWithPrefix 根据key前缀读取数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:01 上午 2021/11/24
|
||||
func TestGetWithPrefix(t *testing.T) {
|
||||
prefix := "/test/dir/"
|
||||
for i := 0; i < 10; i++ {
|
||||
_ = Put(nil, fmt.Sprintf("%s%d", prefix, i), util.GenRandomString("", 8), 0)
|
||||
}
|
||||
fmt.Println(GetWithPrefix(nil, prefix, 0))
|
||||
}
|
||||
|
||||
// TestWatchKey ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
|
Loading…
Reference in New Issue
Block a user