From 7eb9a721f764a2787d501066f1acf7bd530eb13e Mon Sep 17 00:00:00 2001 From: "doubleycn@qq.com" Date: Tue, 30 Jun 2020 18:07:12 +0800 Subject: [PATCH] fix bug https://github.com/micro/go-plugins/issues/565 (#566) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 杨羽 --- redis.go | 57 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/redis.go b/redis.go index b5bf641..e64c5a2 100644 --- a/redis.go +++ b/redis.go @@ -33,31 +33,52 @@ func (r *rkv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error o(&options) } - records := make([]*store.Record, 0, 1) + var keys []string rkey := fmt.Sprintf("%s%s", options.Table, key) - val, err := r.Client.Get(rkey).Bytes() + // Handle Prefix + // TODO suffix + if options.Prefix { + prefix_key := fmt.Sprintf("%s*", rkey) + r.Client.Keys(key) + fkeys, err := r.Client.Keys(prefix_key).Result() + if err != nil { + return nil, err + } + // TODO Limit Offset - if err != nil && err == redis.Nil { - return nil, store.ErrNotFound - } else if err != nil { - return nil, err + keys = append(keys, fkeys...) + + } else { + keys = []string{rkey} } - if val == nil { - return nil, store.ErrNotFound - } + records := make([]*store.Record, 0, len(keys)) - d, err := r.Client.TTL(rkey).Result() - if err != nil { - return nil, err - } + for _, rkey = range keys { + val, err := r.Client.Get(rkey).Bytes() - records = append(records, &store.Record{ - Key: key, - Value: val, - Expiry: d, - }) + if err != nil && err == redis.Nil { + return nil, store.ErrNotFound + } else if err != nil { + return nil, err + } + + if val == nil { + return nil, store.ErrNotFound + } + + d, err := r.Client.TTL(rkey).Result() + if err != nil { + return nil, err + } + + records = append(records, &store.Record{ + Key: key, + Value: val, + Expiry: d, + }) + } return records, nil }