Skip to content

Commit

Permalink
Merge pull request #167 from brzyangg/master
Browse files Browse the repository at this point in the history
add redis pipeline
  • Loading branch information
niubell authored May 11, 2020
2 parents 6e44662 + fa05be7 commit 87d4d75
Show file tree
Hide file tree
Showing 16 changed files with 1,201 additions and 24 deletions.
6 changes: 5 additions & 1 deletion cache/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ func (c ConfigerType) String() string {
}
}

const DefaultRouteGroup = "default"
const (
DefaultRouteGroup = "default"

DefaultRedisWrapper = ""
)
15 changes: 11 additions & 4 deletions cache/redis/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"runtime"
"strconv"
"strings"
"sync"

Expand All @@ -25,22 +26,27 @@ type InstanceConf struct {
Group string
Namespace string
Wrapper string
NoFixKey bool
}

func (m *InstanceConf) String() string {
return fmt.Sprintf("group:%s namespace:%s wrapper:%s", m.Group, m.Namespace, m.Wrapper)
return fmt.Sprintf("group:%s namespace:%s wrapper:%s no_fix:%v", m.Group, m.Namespace, m.Wrapper, m.NoFixKey)
}

func instanceConfFromString(s string) (conf *InstanceConf, err error) {
items := strings.Split(s, keySep)
if len(items) != 3 {
if len(items) != 4 {
return nil, fmt.Errorf("invalid instance conf string:%s", s)
}

noFix, err := strconv.ParseBool(items[3])
if err != nil {
return nil, fmt.Errorf("instance conf parse no_prefix:%s, err: %v", s, err)
}
conf = &InstanceConf{
Group: items[0],
Namespace: items[1],
Wrapper: items[2],
NoFixKey: noFix,
}
return conf, nil
}
Expand All @@ -61,6 +67,7 @@ func (m *InstanceManager) buildKey(conf *InstanceConf) string {
conf.Group,
conf.Namespace,
conf.Wrapper,
fmt.Sprint(conf.NoFixKey),
}, keySep)
}

Expand All @@ -69,7 +76,7 @@ func (m *InstanceManager) add(key string, client *Client) {
}

func (m *InstanceManager) newInstance(ctx context.Context, conf *InstanceConf) (*Client, error) {
return NewClient(ctx, conf.Namespace, conf.Wrapper)
return NewClientWithOptions(ctx, conf.Namespace, WithWrapper(conf.Wrapper), WithNoFixKey(conf.NoFixKey))
}

func (m *InstanceManager) GetInstance(ctx context.Context, conf *InstanceConf) (*Client, error) {
Expand Down
44 changes: 44 additions & 0 deletions cache/redis/instance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package redis

import (
"reflect"
"testing"
)

func Test_instanceConfFromString(t *testing.T) {
type args struct {
s string
}
tests := []struct {
name string
args args
wantConf *InstanceConf
wantErr bool
}{
{
name: "test no prefix key",
args: args{
s: "default-base/test-e-false",
},
wantConf: &InstanceConf{
Group: "default",
Namespace: "base/test",
Wrapper: "e",
NoFixKey: false,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotConf, err := instanceConfFromString(tt.args.s)
if (err != nil) != tt.wantErr {
t.Errorf("instanceConfFromString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotConf, tt.wantConf) {
t.Errorf("instanceConfFromString() gotConf = %v, want %v", gotConf, tt.wantConf)
}
})
}
}
45 changes: 45 additions & 0 deletions cache/redis/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package redis

// redis Client options
type options struct {
// fix key #{namespace.wrapper.key}
wrapper string
// if true no fix key
noFixKey bool
// if true key => #{namespace.wrapper.key} else key => #{namespace.key}
useWrapper bool
}

type Option interface {
apply(*options)
}

type wrapperOption string

func (c wrapperOption) apply(opts *options) {
opts.wrapper = string(c)
}

func WithWrapper(w string) Option {
return wrapperOption(w)
}

type noFixKeyOption bool

func (c noFixKeyOption) apply(opts *options) {
opts.noFixKey = bool(c)
}

func WithNoFixKey(n bool) Option {
return noFixKeyOption(n)
}

type useWrapperOption bool

func (c useWrapperOption) apply(opts *options) {
opts.useWrapper = bool(c)
}

func WithUseWrapper(n bool) Option {
return useWrapperOption(n)
}
Loading

0 comments on commit 87d4d75

Please sign in to comment.