Skip to content

Commit

Permalink
Refactored DisableEmptyValueHit
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnes committed Sep 4, 2023
1 parent 9147392 commit 44c231f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion xload/providers/cached/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func Example_disableEmptyValueHit() {
cached.NewLoader(
remoteLoader,
cached.TTL(5*60*time.Minute),
cached.DisableEmptyValueHit(),
cached.DisableEmptyValueHit,
),
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion xload/providers/cached/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewLoader(l xload.Loader, opts ...Option) xload.LoaderFunc {
return "", err
}

if loaded == "" && !o.emptyValueHit {
if loaded == "" && !o.emptyHit {
return "", nil
}

Expand Down
2 changes: 1 addition & 1 deletion xload/providers/cached/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestNewLoader_WithDisableEmptyValueHit(t *testing.T) {
mc.On("Get", mock.Anything).Return("", nil).Times(6)
mc.On("Set", mock.Anything, mock.Anything, ttl).Return(nil).Times(4)

cachedLoader := NewLoader(loader, TTL(ttl), Cache(mc), DisableEmptyValueHit())
cachedLoader := NewLoader(loader, TTL(ttl), Cache(mc), DisableEmptyValueHit)

cfg := config{}

Expand Down
18 changes: 10 additions & 8 deletions xload/providers/cached/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ func Cache(c Cacher) Option {
}

// DisableEmptyValueHit disables caching of empty values.
func DisableEmptyValueHit() Option {
return optionFunc(func(o *options) { o.emptyValueHit = false })
}
var DisableEmptyValueHit = emptyValHit(false)

type emptyValHit bool

func (e emptyValHit) apply(o *options) { o.emptyHit = bool(e) }

type options struct {
ttl time.Duration
cache Cacher
emptyValueHit bool
ttl time.Duration
cache Cacher
emptyHit bool
}

func defaultOptions() *options {
return &options{
ttl: 5 * time.Minute,
emptyValueHit: true,
ttl: 5 * time.Minute,
emptyHit: true,
}
}

Expand Down

0 comments on commit 44c231f

Please sign in to comment.