Using internal redisClient interface
@@ -2,12 +2,10 @@ package redis
import (
"context"
- "errors"
"fmt"
- "github.com/go-redis/redis/v8"
-
"github.com/dapr/components-contrib/configuration"
+ rediscomponent "github.com/dapr/components-contrib/internal/component/redis"
"github.com/dapr/components-contrib/tests/utils/configupdater"
"github.com/dapr/kit/logger"
)
@@ -15,17 +13,12 @@ import (
const (
host = "redisHost"
password = "redisPassword"
- defaultDB = 0
separator = "||"
)
-type metadata struct {
- Host string
- Password string
-}
-
type ConfigUpdater struct {
- client redis.UniversalClient
+ client rediscomponent.RedisClient
+ clientSettings *rediscomponent.Settings
logger logger.Logger
}
@@ -36,21 +29,6 @@ func NewRedisConfigUpdater(logger logger.Logger) configupdater.Updater {
}
}
-func parseRedisMetadata(props map[string]string) (metadata, error) {
- m := metadata{}
-
- if val, ok := props[host]; ok && val != "" {
- m.Host = val
- } else {
- return m, errors.New("redis store error: missing host address")
- }
-
- if val, ok := props[password]; ok {
- m.Password = val
- }
- return m, nil
-}
-
func getRedisValueFromItem(item *configuration.Item) string {
val := item.Value + separator + item.Version
return val
@@ -68,21 +46,14 @@ func getRedisValuesFromItems(items map[string]*configuration.Item) map[string]st
}
func (r *ConfigUpdater) Init(props map[string]string) error {
- m, err := parseRedisMetadata(props)
+ var err error
+ r.client, r.clientSettings, err = rediscomponent.ParseClientFromProperties(props, nil)
if err != nil {
return err
}
- opts := &redis.Options{
- Addr: m.Host,
- Password: m.Password,
- DB: defaultDB,
- }
-
- r.client = redis.NewClient(opts)
-
- if _, err = r.client.Ping(context.TODO()).Result(); err != nil {
- return fmt.Errorf("redis store: error connecting to redis at %s: %s", m.Host, err)
+ if _, err = r.client.PingResult(context.TODO()); err != nil {
+ return fmt.Errorf("redis store: error connecting to redis at %s: %s", r.clientSettings.Host, err)
}
return nil
@@ -91,17 +62,22 @@ func (r *ConfigUpdater) Init(props map[string]string) error {
func (r *ConfigUpdater) AddKey(items map[string]*configuration.Item) error {
values := getRedisValuesFromItems(items)
- err := r.client.MSet(context.Background(), values).Err()
-
+ for key, val := range values {
+ err := r.client.DoWrite(context.Background(), "SET", key, val)
+ if err != nil {
return err
}
+ }
+
+ return nil
+}
func (r *ConfigUpdater) UpdateKey(items map[string]*configuration.Item) error {
return r.AddKey(items)
}
func (r *ConfigUpdater) DeleteKey(keys []string) error {
- err := r.client.Del(context.Background(), keys...).Err()
+ err := r.client.Del(context.Background(), keys...)
return err
}
