Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type itemOptions struct {
// WithExpiration is an option to set expiration time for any items.
// If the expiration is zero or negative value, it treats as w/o expiration.
func WithExpiration(exp time.Duration) ItemOption {
if exp <= 0 {
return func(o *itemOptions) {}
}
return func(o *itemOptions) {
o.expiration = nowFunc().Add(exp)
}
Expand Down
24 changes: 24 additions & 0 deletions cache_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,30 @@ func TestDeleteExpired(t *testing.T) {
t.Errorf("want %d items but got %d", want, got)
}
})

t.Run("issue #64", func(t *testing.T) {
defer restore()
c := New[string, int]()
c.Set("1", 4, WithExpiration(0)) // These should not be expired
c.Set("2", 5, WithExpiration(-1)) // These should not be expired
c.Set("3", 6, WithExpiration(1*time.Hour))

want := true
_, ok := c.Get("1")
if ok != want {
t.Errorf("want %t but got %t", want, ok)
}

_, ok = c.Get("2")
if ok != want {
t.Errorf("want %t but got %t", want, ok)
}
_, ok = c.Get("3")
if ok != want {
t.Errorf("want %t but got %t", want, ok)
}

})
}

func max(x, y int) int {
Expand Down
Loading