Co-authored-by: 杨羽 <yangyu@doumi.com>
This commit is contained in:
doubleycn@qq.com 2020-06-30 18:07:12 +08:00 committed by Vasiliy Tolstov
parent d6353efc12
commit 7eb9a721f7

View File

@ -33,31 +33,52 @@ func (r *rkv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error
o(&options) o(&options)
} }
records := make([]*store.Record, 0, 1) var keys []string
rkey := fmt.Sprintf("%s%s", options.Table, key) 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 { keys = append(keys, fkeys...)
return nil, store.ErrNotFound
} else if err != nil { } else {
return nil, err keys = []string{rkey}
} }
if val == nil { records := make([]*store.Record, 0, len(keys))
return nil, store.ErrNotFound
}
d, err := r.Client.TTL(rkey).Result() for _, rkey = range keys {
if err != nil { val, err := r.Client.Get(rkey).Bytes()
return nil, err
}
records = append(records, &store.Record{ if err != nil && err == redis.Nil {
Key: key, return nil, store.ErrNotFound
Value: val, } else if err != nil {
Expiry: d, 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 return records, nil
} }