Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindiu authored and actions-user committed Jul 15, 2020
1 parent abd7ba1 commit c991bea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
5 changes: 1 addition & 4 deletions internal/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ type cache struct {
func New(opts ...Option) (cc Cache, err error) {
c := new(cache)
for _, opt := range append(defaultOpts, opts...) {
err = opt(c)
if err != nil {
return nil, err
}
opt(c)
}
switch c.cacher {
case cacher.GACHE:
Expand Down
27 changes: 26 additions & 1 deletion internal/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,39 @@ func TestNew(t *testing.T) {
},
},
{
name: "return unknown error",
name: "return unknown error when type is unknown",
args: args{
opts: []Option{WithType("unknown")},
},
want: want{
err: errors.ErrInvalidCacherType,
},
},
{
name: "return cache when type is empty",
args: args{
opts: []Option{WithType("")},
},
checkFunc: func(w want, got Cache, err error) error {
if err != nil {
return err
}
if got == nil {
return errors.New("got cache is nil")
}

return nil
},
},
{
name: "return unknown error when type is dummy string",
args: args{
opts: []Option{WithType("dummy")},
},
want: want{
err: errors.ErrInvalidCacherType,
},
},
}

for _, test := range tests {
Expand Down
32 changes: 12 additions & 20 deletions internal/cache/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"context"

"github.com/vdaas/vald/internal/cache/cacher"
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/timeutil"
)

type Option func(*cache) error
type Option func(*cache)

var (
defaultOpts = []Option{
Expand All @@ -36,52 +35,45 @@ var (
)

func WithExpiredHook(f func(context.Context, string)) Option {
return func(c *cache) error {
return func(c *cache) {
if f != nil {
c.expiredHook = f
}
return nil
}
}

func WithType(mo string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(mo) == 0 {
return nil
return
}
m := cacher.ToType(mo)
if m == cacher.Unknown {
return errors.ErrInvalidCacherType
}
c.cacher = m
return nil

c.cacher = cacher.ToType(mo)
}
}

func WithExpireDuration(dur string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(dur) == 0 {
return nil
return
}
d, err := timeutil.Parse(dur)
if err != nil {
return nil
return
}
c.expireDur = d
return nil
}
}

func WithExpireCheckDuration(dur string) Option {
return func(c *cache) error {
return func(c *cache) {
if len(dur) == 0 {
return nil
return
}
d, err := timeutil.Parse(dur)
if err != nil {
return nil
return
}
c.expireCheckDur = d
return nil
}
}

0 comments on commit c991bea

Please sign in to comment.