2019-10-03 11:57:49 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
2020-01-31 01:26:39 +03:00
|
|
|
"github.com/micro/go-micro/v2/store"
|
2019-10-03 11:57:49 +03:00
|
|
|
redis "gopkg.in/redis.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rkv struct {
|
2019-12-16 18:41:04 +03:00
|
|
|
options store.Options
|
|
|
|
Client *redis.Client
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
func (r *rkv) Init(...store.Option) error {
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
func (r *rkv) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) {
|
|
|
|
records := make([]*store.Record, 0, 1)
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
val, err := r.Client.Get(key).Bytes()
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
if err != nil && err == redis.Nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-18 00:00:29 +03:00
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
if val == nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
2019-10-18 00:00:29 +03:00
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
d, err := r.Client.TTL(key).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
|
|
|
Expiry: d,
|
|
|
|
})
|
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
return records, nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
func (r *rkv) Delete(key string) error {
|
|
|
|
return r.Client.Del(key).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2020-02-12 18:53:18 +03:00
|
|
|
func (r *rkv) Write(record *store.Record) error {
|
|
|
|
return r.Client.Set(record.Key, record.Value, record.Expiry).Err()
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-25 19:56:13 +03:00
|
|
|
func (r *rkv) List() ([]*store.Record, error) {
|
2019-10-03 11:57:49 +03:00
|
|
|
keys, err := r.Client.Keys("*").Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-04 13:36:30 +03:00
|
|
|
|
|
|
|
vals := make([]*store.Record, 0, len(keys))
|
2019-10-03 11:57:49 +03:00
|
|
|
for _, k := range keys {
|
|
|
|
i, err := r.Read(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-18 00:00:29 +03:00
|
|
|
vals = append(vals, i...)
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
return vals, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) String() string {
|
|
|
|
return "redis"
|
|
|
|
}
|
|
|
|
|
2019-12-16 18:41:04 +03:00
|
|
|
func NewStore(opts ...store.Option) store.Store {
|
|
|
|
var options store.Options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-12-16 18:41:04 +03:00
|
|
|
nodes := options.Nodes
|
|
|
|
|
2019-10-03 11:57:49 +03:00
|
|
|
if len(nodes) == 0 {
|
|
|
|
nodes = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rkv{
|
2019-12-16 18:41:04 +03:00
|
|
|
options: options,
|
2019-10-03 11:57:49 +03:00
|
|
|
Client: redis.NewClient(&redis.Options{
|
|
|
|
Addr: nodes[0],
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|