store: respect Init options

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-05-13 12:59:23 +03:00
parent ba80206fb9
commit 7ddb0aab10

View File

@ -3,6 +3,7 @@ package redis
import ( import (
"fmt" "fmt"
log "github.com/micro/go-micro/v2/logger"
"github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/store"
redis "gopkg.in/redis.v5" redis "gopkg.in/redis.v5"
) )
@ -12,8 +13,12 @@ type rkv struct {
Client *redis.Client Client *redis.Client
} }
func (r *rkv) Init(...store.Option) error { func (r *rkv) Init(opts ...store.Option) error {
return nil for _, o := range opts {
o(&r.options)
}
return r.configure()
} }
func (r *rkv) Close() error { func (r *rkv) Close() error {
@ -83,18 +88,28 @@ func NewStore(opts ...store.Option) store.Store {
o(&options) o(&options)
} }
nodes := options.Nodes s := new(rkv)
s.options = options
if err := s.configure(); err != nil {
log.Fatal(err)
}
return s
}
func (r *rkv) configure() error {
nodes := r.options.Nodes
if len(nodes) == 0 { if len(nodes) == 0 {
nodes = []string{"127.0.0.1:6379"} nodes = []string{"127.0.0.1:6379"}
} }
return &rkv{ r.Client = redis.NewClient(&redis.Options{
options: options,
Client: redis.NewClient(&redis.Options{
Addr: nodes[0], Addr: nodes[0],
Password: "", // no password set Password: "", // no password set
DB: 0, // use default DB DB: 0, // use default DB
}), })
}
return nil
} }