Compare commits

..

2 Commits

Author SHA1 Message Date
2245314c2f Merge pull request 'add ability to get *redis.Client' (#110) from redis into v3
Some checks failed
build / test (push) Failing after 1m28s
build / lint (push) Failing after 2m35s
codeql / analyze (go) (push) Failing after 2m48s
Reviewed-on: #110
2023-12-12 13:49:14 +03:00
db770c3fe7 add ability to get *redis.Client
Some checks failed
codeql / analyze (go) (pull_request) Failing after 2m48s
prbuild / test (pull_request) Failing after 1m30s
prbuild / lint (pull_request) Failing after 2m37s
autoapprove / autoapprove (pull_request) Failing after 1m26s
automerge / automerge (pull_request) Failing after 4s
dependabot-automerge / automerge (pull_request) Has been skipped
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-12-12 13:48:47 +03:00
5 changed files with 34 additions and 32 deletions

4
go.mod
View File

@@ -1,10 +1,10 @@
module go.unistack.org/micro-store-redis/v4
module go.unistack.org/micro-store-redis/v3
go 1.20
require (
github.com/redis/go-redis/v9 v9.2.1
go.unistack.org/micro/v4 v4.0.7
go.unistack.org/micro/v3 v3.10.28
)
require (

4
go.sum
View File

@@ -8,5 +8,5 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
go.unistack.org/micro/v4 v4.0.7 h1:2lwtZlHcSwgkahhFbkI4x1lOS79lw8uLHtcEhlFF+AM=
go.unistack.org/micro/v4 v4.0.7/go.mod h1:bVEYTlPi0EsdgZZt311bIroDg9ict7ky3C87dSCCAGk=
go.unistack.org/micro/v3 v3.10.28 h1:/87lGekrmi0/66pioy+Nh8lVUBBYnVqKoHiNYX5OmMI=
go.unistack.org/micro/v3 v3.10.28/go.mod h1:eUgtvbtiiz6te93m0ZdmoecbitWwjdBmmr84srmEIKA=

View File

@@ -2,17 +2,17 @@ package redis
import (
"github.com/redis/go-redis/v9"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v3/store"
)
type configKey struct{}
func Config(c *redis.Options) options.Option {
return options.ContextOption(configKey{}, c)
func Config(c *redis.Options) store.Option {
return store.SetOption(configKey{}, c)
}
type clusterConfigKey struct{}
func ClusterConfig(c *redis.ClusterOptions) options.Option {
return options.ContextOption(clusterConfigKey{}, c)
func ClusterConfig(c *redis.ClusterOptions) store.Option {
return store.SetOption(clusterConfigKey{}, c)
}

View File

@@ -8,8 +8,7 @@ import (
"time"
"github.com/redis/go-redis/v9"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/store"
"go.unistack.org/micro/v3/store"
)
type Store struct {
@@ -35,7 +34,7 @@ func (r *Store) Connect(ctx context.Context) error {
return r.cli.Ping(ctx).Err()
}
func (r *Store) Init(opts ...options.Option) error {
func (r *Store) Init(opts ...store.Option) error {
for _, o := range opts {
o(&r.opts)
}
@@ -43,11 +42,15 @@ func (r *Store) Init(opts ...options.Option) error {
return r.configure()
}
func (r *Store) Redis() *redis.Client {
return r.cli.(*redis.Client)
}
func (r *Store) Disconnect(ctx context.Context) error {
return r.cli.Close()
}
func (r *Store) Exists(ctx context.Context, key string, opts ...options.Option) error {
func (r *Store) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
if r.opts.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
@@ -70,7 +73,7 @@ func (r *Store) Exists(ctx context.Context, key string, opts ...options.Option)
return nil
}
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
if r.opts.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
@@ -96,7 +99,7 @@ func (r *Store) Read(ctx context.Context, key string, val interface{}, opts ...o
return r.opts.Codec.Unmarshal(buf, val)
}
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...options.Option) error {
func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts ...store.ReadOption) error {
if len(keys) == 1 {
vt := reflect.ValueOf(vals)
if vt.Kind() == reflect.Ptr {
@@ -171,7 +174,7 @@ func (r *Store) MRead(ctx context.Context, keys []string, vals interface{}, opts
return nil
}
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...options.Option) error {
func (r *Store) MDelete(ctx context.Context, keys []string, opts ...store.DeleteOption) error {
if len(keys) == 1 {
return r.Delete(ctx, keys[0], opts...)
}
@@ -193,7 +196,7 @@ func (r *Store) MDelete(ctx context.Context, keys []string, opts ...options.Opti
return r.cli.Del(ctx, keys...).Err()
}
func (r *Store) Delete(ctx context.Context, key string, opts ...options.Option) error {
func (r *Store) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
if r.opts.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
@@ -209,7 +212,7 @@ func (r *Store) Delete(ctx context.Context, key string, opts ...options.Option)
return r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
}
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...options.Option) error {
func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, opts ...store.WriteOption) error {
if len(keys) == 1 {
return r.Write(ctx, keys[0], vals[0], opts...)
}
@@ -265,7 +268,7 @@ func (r *Store) MWrite(ctx context.Context, keys []string, vals []interface{}, o
return nil
}
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...options.Option) error {
func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
if r.opts.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
@@ -298,7 +301,7 @@ func (r *Store) Write(ctx context.Context, key string, val interface{}, opts ...
return r.cli.Set(ctx, key, buf, options.TTL).Err()
}
func (r *Store) List(ctx context.Context, opts ...options.Option) ([]string, error) {
func (r *Store) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
options := store.NewListOptions(opts...)
if len(options.Namespace) == 0 {
options.Namespace = r.opts.Namespace
@@ -341,7 +344,7 @@ func (r *Store) String() string {
return "redis"
}
func NewStore(opts ...options.Option) *Store {
func NewStore(opts ...store.Option) *Store {
return &Store{opts: store.NewOptions(opts...)}
}
@@ -350,7 +353,7 @@ func (r *Store) configure() error {
var redisClusterOptions *redis.ClusterOptions
var err error
nodes := r.opts.Address
nodes := r.opts.Addrs
if len(nodes) == 0 {
nodes = []string{"redis://127.0.0.1:6379"}

View File

@@ -8,8 +8,7 @@ import (
"time"
"github.com/redis/go-redis/v9"
"go.unistack.org/micro/v4/options"
"go.unistack.org/micro/v4/store"
"go.unistack.org/micro/v3/store"
)
func Test_rkv_configure(t *testing.T) {
@@ -38,7 +37,7 @@ func Test_rkv_configure(t *testing.T) {
},
},
{
name: "legacy Url", fields: fields{options: store.Options{Address: []string{"127.0.0.1:6379"}}, Client: nil},
name: "legacy Url", fields: fields{options: store.Options{Addrs: []string{"127.0.0.1:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "",
@@ -46,7 +45,7 @@ func Test_rkv_configure(t *testing.T) {
},
},
{
name: "New Url", fields: fields{options: store.Options{Address: []string{"redis://127.0.0.1:6379"}}, Client: nil},
name: "New Url", fields: fields{options: store.Options{Addrs: []string{"redis://127.0.0.1:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "",
@@ -54,7 +53,7 @@ func Test_rkv_configure(t *testing.T) {
},
},
{
name: "Url with Pwd", fields: fields{options: store.Options{Address: []string{"redis://:password@redis:6379"}}, Client: nil},
name: "Url with Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://:password@redis:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "",
password: "password",
@@ -62,7 +61,7 @@ func Test_rkv_configure(t *testing.T) {
},
},
{
name: "Url with username and Pwd", fields: fields{options: store.Options{Address: []string{"redis://username:password@redis:6379"}}, Client: nil},
name: "Url with username and Pwd", fields: fields{options: store.Options{Addrs: []string{"redis://username:password@redis:6379"}}, Client: nil},
wantErr: false, want: wantValues{
username: "username",
password: "password",
@@ -72,11 +71,11 @@ func Test_rkv_configure(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &Store{
rc := &Store{
opts: tt.fields.options,
cli: tt.fields.Client,
}
err := r.configure()
err := rc.configure()
if (err != nil) != tt.wantErr {
t.Errorf("configure() error = %v, wantErr %v", err, tt.wantErr)
return
@@ -91,7 +90,7 @@ func Test_Store(t *testing.T) {
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
t.Skip()
}
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
if err := r.Init(); err != nil {
t.Fatal(err)
@@ -131,7 +130,7 @@ func Test_MRead(t *testing.T) {
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
t.Skip()
}
r := NewStore(options.Address(os.Getenv("STORE_NODES")))
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
if err = r.Init(); err != nil {
t.Fatal(err)