Skip to content

Commit

Permalink
Fix zero value Cache default expiration (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
erni27 committed May 6, 2023
1 parent 8231e75 commit 5b7d897
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions imcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type Cache[K comparable, V any] struct {
func (s *Cache[K, V]) init() {
if s.m == nil {
s.m = make(map[K]entry[K, V])
s.defaultExp = noExp
s.queue = &nopq[K]{}
}
}
Expand Down
11 changes: 9 additions & 2 deletions imcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1545,8 +1545,15 @@ func TestNewSharded_NilHasher(t *testing.T) {
func TestCache_ZeroValue(t *testing.T) {
var c Cache[string, string]
c.Set("foo", "bar", WithNoExpiration())
if v, ok := c.Get("foo"); !ok || v != "bar" {
t.Errorf("want Cache.Get(_) = %s, true, got %s, %t", "bar", v, ok)
if got, ok := c.Get("foo"); !ok || got != "bar" {
t.Errorf("Cache.Get(%s) = %s, %t, want %s, %t", "foo", got, ok, "bar", true)
}
// Make sure the zero value Cache has default expiration
// set to no expiration.
c.Set("bar", "foo", WithDefaultExpiration())
<-time.After(time.Millisecond)
if got, ok := c.Get("bar"); !ok || got != "foo" {
t.Errorf("Cache.Get(%s) = %s, %t, want %s, %t", "bar", got, ok, "foo", true)
}
}

Expand Down

0 comments on commit 5b7d897

Please sign in to comment.