2019-10-03 11:57:49 +03:00
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/micro/go-micro/config/options"
|
|
|
|
"github.com/micro/go-micro/store"
|
|
|
|
redis "gopkg.in/redis.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rkv struct {
|
|
|
|
options.Options
|
|
|
|
Client *redis.Client
|
|
|
|
}
|
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
func (r *rkv) Read(keys ...string) ([]*store.Record, error) {
|
|
|
|
var records []*store.Record
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
for _, key := range keys {
|
|
|
|
val, err := r.Client.Get(key).Bytes()
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
if err != nil && err == redis.Nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-10-03 11:57:49 +03:00
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
if val == nil {
|
|
|
|
return nil, store.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := r.Client.TTL(key).Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, &store.Record{
|
|
|
|
Key: key,
|
|
|
|
Value: val,
|
|
|
|
Expiry: d,
|
|
|
|
})
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
return records, nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
func (r *rkv) Delete(keys ...string) error {
|
|
|
|
var err error
|
|
|
|
for _, key := range keys {
|
|
|
|
if err = r.Client.Del(key).Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
2019-10-18 00:00:29 +03:00
|
|
|
func (r *rkv) Write(records ...*store.Record) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
err = r.Client.Set(record.Key, record.Value, record.Expiry).Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-10-03 11:57:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rkv) Sync() ([]*store.Record, error) {
|
|
|
|
keys, err := r.Client.Keys("*").Result()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var vals []*store.Record
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStore(opts ...options.Option) store.Store {
|
|
|
|
options := options.NewOptions(opts...)
|
|
|
|
|
|
|
|
var nodes []string
|
|
|
|
|
|
|
|
if n, ok := options.Values().Get("store.nodes"); ok {
|
|
|
|
nodes = n.([]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
nodes = []string{"127.0.0.1:6379"}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rkv{
|
|
|
|
Options: options,
|
|
|
|
Client: redis.NewClient(&redis.Options{
|
|
|
|
Addr: nodes[0],
|
|
|
|
Password: "", // no password set
|
|
|
|
DB: 0, // use default DB
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|