From 1da57931322e142bb3e1493546f3933345e82a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 24 Nov 2021 11:26:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=95=B0=E6=8D=AE=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8C=89=E7=85=A7=E5=89=8D=E7=BC=80=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- middleware/etcd/string.go | 48 +++++++++++++++++++++++++++++----- middleware/etcd/string_test.go | 15 +++++++++++ 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 273f21e..4bad366 100644 --- a/go.mod +++ b/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 diff --git a/middleware/etcd/string.go b/middleware/etcd/string.go index 9186483..49d7088 100644 --- a/middleware/etcd/string.go +++ b/middleware/etcd/string.go @@ -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 +} diff --git a/middleware/etcd/string_test.go b/middleware/etcd/string_test.go index 21c2805..46ae1da 100644 --- a/middleware/etcd/string_test.go +++ b/middleware/etcd/string_test.go @@ -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<白茶清欢>