Compare commits
29 Commits
Author | SHA1 | Date | |
---|---|---|---|
fcab95bd8a | |||
ede2cf695c | |||
|
755d601c94 | ||
164969611a | |||
|
e0d526588b | ||
|
990d87c946 | ||
1e3f33dab8 | |||
|
28ba5d2c25 | ||
62973372d6 | |||
|
7b820e6b02 | ||
abab4e5428 | |||
|
1d72797e95 | ||
627e3fdb94 | |||
|
1ad6cbfb2e | ||
5ff5b6e018 | |||
|
0b9be86b6b | ||
a23f2dead7 | |||
|
c76675c4d3 | ||
|
64e8a8a6bd | ||
3532091dc7 | |||
|
2cda4e9af7 | ||
|
c182715e3e | ||
|
4c3c2f880d | ||
3c726fb85e | |||
95ac858c36 | |||
25b637a199 | |||
edb352fd48 | |||
|
1e78c57dc5 | ||
|
fedc7ad843 |
2
.github/workflows/autoapprove.yml
vendored
2
.github/workflows/autoapprove.yml
vendored
@@ -13,7 +13,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: approve
|
||||
uses: hmarr/auto-approve-action@v2
|
||||
uses: hmarr/auto-approve-action@v3
|
||||
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
|
||||
id: approve
|
||||
with:
|
||||
|
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -34,7 +34,7 @@ jobs:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.2.0
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
2
.github/workflows/dependabot-automerge.yml
vendored
2
.github/workflows/dependabot-automerge.yml
vendored
@@ -15,7 +15,7 @@ jobs:
|
||||
steps:
|
||||
- name: metadata
|
||||
id: metadata
|
||||
uses: dependabot/fetch-metadata@v1.3.3
|
||||
uses: dependabot/fetch-metadata@v1.3.6
|
||||
with:
|
||||
github-token: "${{ secrets.TOKEN }}"
|
||||
- name: merge
|
||||
|
2
.github/workflows/pr.yml
vendored
2
.github/workflows/pr.yml
vendored
@@ -34,7 +34,7 @@ jobs:
|
||||
- name: checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: lint
|
||||
uses: golangci/golangci-lint-action@v3.2.0
|
||||
uses: golangci/golangci-lint-action@v3.4.0
|
||||
continue-on-error: true
|
||||
with:
|
||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||
|
3
go.mod
3
go.mod
@@ -4,8 +4,7 @@ go 1.16
|
||||
|
||||
require (
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/google/gnostic v0.6.9 // indirect
|
||||
go.unistack.org/micro/v3 v3.9.11
|
||||
go.unistack.org/micro/v3 v3.10.5
|
||||
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
18
go.sum
18
go.sum
@@ -52,9 +52,6 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
|
||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||
github.com/google/gnostic v0.6.8 h1:bT56GPYBWh1tvBuBEd94qcS3+60b+y0HQur0ITkGuCk=
|
||||
github.com/google/gnostic v0.6.8/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||
github.com/google/gnostic v0.6.9 h1:ZK/5VhkoX835RikCHpSUJV9a+S3e1zLh59YnyWeBW+0=
|
||||
github.com/google/gnostic v0.6.9/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
@@ -108,12 +105,10 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
|
||||
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
|
||||
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
|
||||
go.unistack.org/micro/v3 v3.9.10 h1:MOLOM8sm3PthhkJVKiraUlOMDyQLAofKNCJakPN/JBw=
|
||||
go.unistack.org/micro/v3 v3.9.10/go.mod h1:LrBm9Fsf4MbY8DlUbdwRTYJosMkN8wrtOQHoYBlMkz8=
|
||||
go.unistack.org/micro/v3 v3.9.11 h1:dsZVss3nvfByL1ZDJNnUVQB1N8w6qn4pr9vIkeBiii8=
|
||||
go.unistack.org/micro/v3 v3.9.11/go.mod h1:LrBm9Fsf4MbY8DlUbdwRTYJosMkN8wrtOQHoYBlMkz8=
|
||||
go.unistack.org/micro-proto/v3 v3.3.1 h1:nQ0MtWvP2G3QrpOgawVOPhpZZYkq6umTGDqs8FxJYIo=
|
||||
go.unistack.org/micro-proto/v3 v3.3.1/go.mod h1:cwRyv8uInM2I7EbU7O8Fx2Ls3N90Uw9UCCcq4olOdfE=
|
||||
go.unistack.org/micro/v3 v3.10.5 h1:m71XxBa3eOK83bBof0qGU1Fkfo0zYY7gXrtjdE7S6U4=
|
||||
go.unistack.org/micro/v3 v3.10.5/go.mod h1:gI4RkJKHLPW7KV6h4+ZBOZD997MRvFRXMPQIHpozikI=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
@@ -209,8 +204,8 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
|
||||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
@@ -225,7 +220,6 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA=
|
||||
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
165
redis.go
Normal file → Executable file
165
redis.go
Normal file → Executable file
@@ -3,6 +3,8 @@ package redis // import "go.unistack.org/micro-store-redis/v3"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@@ -14,18 +16,20 @@ type rkv struct {
|
||||
cli redisClient
|
||||
}
|
||||
|
||||
// TODO: add ability to set some redis options https://pkg.go.dev/github.com/go-redis/redis/v8#Options
|
||||
type redisClient interface {
|
||||
Get(ctx context.Context, key string) *redis.StringCmd
|
||||
Del(ctx context.Context, keys ...string) *redis.IntCmd
|
||||
Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd
|
||||
Keys(ctx context.Context, pattern string) *redis.StringSliceCmd
|
||||
MGet(ctx context.Context, keys ...string) *redis.SliceCmd
|
||||
MSet(ctx context.Context, kv ...interface{}) *redis.StatusCmd
|
||||
Exists(ctx context.Context, keys ...string) *redis.IntCmd
|
||||
Ping(ctx context.Context) *redis.StatusCmd
|
||||
Close() error
|
||||
}
|
||||
|
||||
func (r *rkv) Connect(ctx context.Context) error {
|
||||
return nil
|
||||
return r.cli.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func (r *rkv) Init(opts ...store.Option) error {
|
||||
@@ -45,17 +49,19 @@ func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
if options.Namespace != "" {
|
||||
key = fmt.Sprintf("%s%s", r.opts.Namespace, key)
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
st, err := r.cli.Exists(ctx, rkey).Result()
|
||||
val, err := r.cli.Exists(ctx, key).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if st == 0 {
|
||||
if val == 0 {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
@@ -66,13 +72,15 @@ func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...sto
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
if options.Namespace != "" {
|
||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
buf, err := r.cli.Get(ctx, rkey).Bytes()
|
||||
buf, err := r.cli.Get(ctx, key).Bytes()
|
||||
if err != nil && err == redis.Nil {
|
||||
return store.ErrNotFound
|
||||
} else if err != nil {
|
||||
@@ -81,20 +89,81 @@ func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...sto
|
||||
if buf == nil {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
/*
|
||||
d, err := r.Client.TTL(rkey).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return r.opts.Codec.Unmarshal(buf, val)
|
||||
}
|
||||
|
||||
func (r *rkv) 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 {
|
||||
vt = reflect.Indirect(vt)
|
||||
return r.Read(ctx, keys[0], vt.Index(0).Interface(), opts...)
|
||||
}
|
||||
}
|
||||
options := store.NewReadOptions(opts...)
|
||||
rkeys := make([]string, 0, len(keys))
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
for _, key := range keys {
|
||||
if options.Namespace != "" {
|
||||
rkeys = append(rkeys, fmt.Sprintf("%s%s", options.Namespace, key))
|
||||
} else {
|
||||
rkeys = append(rkeys, key)
|
||||
}
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
rvals, err := r.cli.MGet(ctx, rkeys...).Result()
|
||||
if err != nil && err == redis.Nil {
|
||||
return store.ErrNotFound
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(rvals) == 0 {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
|
||||
records = append(records, &store.Record{
|
||||
Key: key,
|
||||
Value: val,
|
||||
Expiry: d,
|
||||
})
|
||||
vv := reflect.ValueOf(vals)
|
||||
vt := reflect.TypeOf(vals)
|
||||
switch vv.Kind() {
|
||||
case reflect.Ptr:
|
||||
vv = reflect.Indirect(vv)
|
||||
vt = vt.Elem()
|
||||
}
|
||||
*/
|
||||
return r.opts.Codec.Unmarshal(buf, val)
|
||||
if vv.Kind() != reflect.Slice {
|
||||
return store.ErrNotFound
|
||||
}
|
||||
nvv := reflect.MakeSlice(vt, len(rvals), len(rvals))
|
||||
vt = vt.Elem()
|
||||
for idx := 0; idx < len(rvals); idx++ {
|
||||
if rvals[idx] == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
itm := nvv.Index(idx)
|
||||
var buf []byte
|
||||
switch b := rvals[idx].(type) {
|
||||
case []byte:
|
||||
buf = b
|
||||
case string:
|
||||
buf = []byte(b)
|
||||
}
|
||||
// special case for raw data
|
||||
if vt.Kind() == reflect.Slice && vt.Elem().Kind() == reflect.Uint8 {
|
||||
itm.Set(reflect.MakeSlice(itm.Type(), len(buf), len(buf)))
|
||||
} else {
|
||||
itm.Set(reflect.New(vt.Elem()))
|
||||
}
|
||||
if err = r.opts.Codec.Unmarshal(buf, itm.Interface()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
vv.Set(nvv)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
||||
@@ -102,13 +171,15 @@ func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
if options.Namespace == "" {
|
||||
return r.cli.Del(ctx, key).Err()
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
return r.cli.Del(ctx, rkey).Err()
|
||||
return r.cli.Del(ctx, fmt.Sprintf("%s%s", options.Namespace, key)).Err()
|
||||
}
|
||||
|
||||
func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
||||
@@ -116,18 +187,19 @@ func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...st
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
|
||||
rkey := fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
buf, err := r.opts.Codec.Marshal(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if options.Namespace != "" {
|
||||
key = fmt.Sprintf("%s%s", options.Namespace, key)
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
return r.cli.Set(ctx, rkey, buf, options.TTL).Err()
|
||||
return r.cli.Set(ctx, key, buf, options.TTL).Err()
|
||||
}
|
||||
|
||||
func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
||||
@@ -135,18 +207,30 @@ func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, err
|
||||
if len(options.Namespace) == 0 {
|
||||
options.Namespace = r.opts.Namespace
|
||||
}
|
||||
|
||||
rkey := fmt.Sprintf("%s%s*", options.Namespace, options.Prefix)
|
||||
if options.Suffix != "" {
|
||||
rkey += options.Suffix
|
||||
}
|
||||
if r.opts.Timeout > 0 {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout)
|
||||
defer cancel()
|
||||
}
|
||||
// TODO: add support for prefix/suffix/limit
|
||||
keys, err := r.cli.Keys(ctx, "*").Result()
|
||||
keys, err := r.cli.Keys(ctx, rkey).Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if options.Namespace == "" {
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
nkeys := make([]string, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
nkeys = append(nkeys, strings.TrimPrefix(key, options.Namespace))
|
||||
}
|
||||
return nkeys, nil
|
||||
}
|
||||
|
||||
func (r *rkv) Options() store.Options {
|
||||
@@ -161,7 +245,7 @@ func (r *rkv) String() string {
|
||||
return "redis"
|
||||
}
|
||||
|
||||
func NewStore(opts ...store.Option) store.Store {
|
||||
func NewStore(opts ...store.Option) *rkv {
|
||||
return &rkv{opts: store.NewOptions(opts...)}
|
||||
}
|
||||
|
||||
@@ -179,10 +263,16 @@ func (r *rkv) configure() error {
|
||||
if r.opts.Context != nil {
|
||||
if c, ok := r.opts.Context.Value(configKey{}).(*redis.Options); ok {
|
||||
redisOptions = c
|
||||
if r.opts.TLSConfig != nil {
|
||||
redisOptions.TLSConfig = r.opts.TLSConfig
|
||||
}
|
||||
}
|
||||
|
||||
if c, ok := r.opts.Context.Value(clusterConfigKey{}).(*redis.ClusterOptions); ok {
|
||||
redisClusterOptions = c
|
||||
if r.opts.TLSConfig != nil {
|
||||
redisClusterOptions.TLSConfig = r.opts.TLSConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,10 +288,31 @@ func (r *rkv) configure() error {
|
||||
Addr: nodes[0],
|
||||
Password: "", // no password set
|
||||
DB: 0, // use default DB
|
||||
MaxRetries: 2,
|
||||
MaxRetryBackoff: 256 * time.Millisecond,
|
||||
DialTimeout: 1 * time.Second,
|
||||
ReadTimeout: 1 * time.Second,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
PoolTimeout: 1 * time.Second,
|
||||
IdleTimeout: 20 * time.Second,
|
||||
MinIdleConns: 10,
|
||||
TLSConfig: r.opts.TLSConfig,
|
||||
}
|
||||
}
|
||||
} else if redisOptions == nil && redisClusterOptions == nil && len(nodes) > 1 {
|
||||
redisClusterOptions = &redis.ClusterOptions{Addrs: nodes}
|
||||
redisClusterOptions = &redis.ClusterOptions{
|
||||
Addrs: nodes,
|
||||
Password: "", // no password set
|
||||
MaxRetries: 2,
|
||||
MaxRetryBackoff: 256 * time.Millisecond,
|
||||
DialTimeout: 1 * time.Second,
|
||||
ReadTimeout: 1 * time.Second,
|
||||
WriteTimeout: 1 * time.Second,
|
||||
PoolTimeout: 1 * time.Second,
|
||||
IdleTimeout: 20 * time.Second,
|
||||
MinIdleConns: 10,
|
||||
TLSConfig: r.opts.TLSConfig,
|
||||
}
|
||||
}
|
||||
|
||||
if redisOptions != nil {
|
||||
|
131
redis_test.go
Normal file → Executable file
131
redis_test.go
Normal file → Executable file
@@ -1,6 +1,7 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -89,33 +90,143 @@ func Test_Store(t *testing.T) {
|
||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||
t.Skip()
|
||||
}
|
||||
r := new(rkv)
|
||||
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||
|
||||
// r.options = store.Options{Nodes: []string{"redis://:password@127.0.0.1:6379"}}
|
||||
// r.options = store.Options{Nodes: []string{"127.0.0.1:6379"}}
|
||||
|
||||
r.opts = store.NewOptions(store.Addrs(os.Getenv("STORE_NODES")))
|
||||
|
||||
if err := r.configure(); err != nil {
|
||||
if err := r.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.Connect(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
key := "myTest"
|
||||
tval := []byte("myValue")
|
||||
var val []byte
|
||||
err := r.Write(ctx, key, []byte("myValue"), store.WriteTTL(2*time.Minute))
|
||||
err := r.Write(ctx, key, tval, store.WriteTTL(2*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Write error: %v", err)
|
||||
}
|
||||
err = r.Read(ctx, key, val)
|
||||
err = r.Read(ctx, key, &val)
|
||||
if err != nil {
|
||||
t.Fatalf("Read error: %v\n", err)
|
||||
} else if !bytes.Equal(val, tval) {
|
||||
t.Fatalf("read err: data not eq")
|
||||
}
|
||||
keys, err := r.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("List error: %v\n", err)
|
||||
}
|
||||
_ = keys
|
||||
err = r.Delete(ctx, key)
|
||||
if err != nil {
|
||||
t.Fatalf("Delete error: %v\n", err)
|
||||
}
|
||||
_, err = r.List(ctx)
|
||||
// t.Logf("%v", keys)
|
||||
}
|
||||
|
||||
func Test_MRead(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||
t.Skip()
|
||||
}
|
||||
r := NewStore(store.Addrs(os.Getenv("STORE_NODES")))
|
||||
|
||||
if err = r.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = r.Connect(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
key1 := "myTest1"
|
||||
key2 := "myTest2"
|
||||
tval1 := []byte("myValue1")
|
||||
tval2 := []byte("myValue2")
|
||||
var vals [][]byte
|
||||
err = r.Write(ctx, key1, tval1, store.WriteTTL(2*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Write error: %v", err)
|
||||
}
|
||||
err = r.Write(ctx, key2, tval2, store.WriteTTL(2*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Write error: %v", err)
|
||||
}
|
||||
err = r.MRead(ctx, []string{key1, key2}, &vals)
|
||||
if err != nil {
|
||||
t.Fatalf("Read error: %v\n", err)
|
||||
}
|
||||
// t.Logf("%s", vals)
|
||||
_ = vals
|
||||
keys, err := r.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("List error: %v\n", err)
|
||||
}
|
||||
_ = keys
|
||||
// t.Logf("%v", keys)
|
||||
err = r.Delete(ctx, key1)
|
||||
if err != nil {
|
||||
t.Fatalf("Delete error: %v\n", err)
|
||||
}
|
||||
err = r.Delete(ctx, key2)
|
||||
if err != nil {
|
||||
t.Fatalf("Delete error: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func Test_MReadCodec(t *testing.T) {
|
||||
type mytype struct {
|
||||
Key string `json:"name"`
|
||||
Val string `json:"val"`
|
||||
}
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
|
||||
t.Skip()
|
||||
}
|
||||
r := NewStore(store.Nodes(os.Getenv("STORE_NODES")), store.Codec(jsoncodec.NewCodec()))
|
||||
|
||||
if err = r.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = r.Connect(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
key1 := "myTest1"
|
||||
key2 := "myTest2"
|
||||
key3 := "myTest3"
|
||||
tval1 := &mytype{Key: "key1", Val: "val1"}
|
||||
tval2 := &mytype{Key: "key2", Val: "val2"}
|
||||
var vals []*mytype
|
||||
err = r.Write(ctx, key1, tval1, store.WriteTTL(2*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Write error: %v", err)
|
||||
}
|
||||
err = r.Write(ctx, key2, tval2, store.WriteTTL(2*time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("Write error: %v", err)
|
||||
}
|
||||
err = r.MRead(ctx, []string{key1, key3, key2}, &vals)
|
||||
if err != nil {
|
||||
t.Fatalf("Read error: %v\n", err)
|
||||
}
|
||||
if vals[0].Key != "key1" || vals[1] != nil || vals[2].Key != "key2" {
|
||||
t.Fatalf("read err: struct not filled")
|
||||
}
|
||||
keys, err := r.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("List error: %v\n", err)
|
||||
}
|
||||
_ = keys
|
||||
err = r.Delete(ctx, key1)
|
||||
if err != nil {
|
||||
t.Fatalf("Delete error: %v\n", err)
|
||||
}
|
||||
err = r.Delete(ctx, key2)
|
||||
if err != nil {
|
||||
t.Fatalf("Delete error: %v\n", err)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user