From cb90d4c078827529706344bb1b0539ec268e842a Mon Sep 17 00:00:00 2001 From: John Nguyen Date: Thu, 23 Jan 2025 14:12:07 +0800 Subject: [PATCH 1/3] add test to check zero expiry --- cache_internal_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cache_internal_test.go b/cache_internal_test.go index 580b726..f0fab1d 100644 --- a/cache_internal_test.go +++ b/cache_internal_test.go @@ -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 { From 62b62f281282a4388d2cf4ad5a00ea8478ef8acb Mon Sep 17 00:00:00 2001 From: John Nguyen Date: Thu, 23 Jan 2025 14:12:18 +0800 Subject: [PATCH 2/3] handle zero expiry --- cache.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cache.go b/cache.go index e9f6dea..6bd54ff 100644 --- a/cache.go +++ b/cache.go @@ -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) } From 6298b3441ae6ee33546dca605bb29a10d3ba9275 Mon Sep 17 00:00:00 2001 From: John Nguyen Date: Sat, 25 Jan 2025 09:24:48 +0800 Subject: [PATCH 3/3] fix tests --- cache_internal_test.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/cache_internal_test.go b/cache_internal_test.go index f0fab1d..9b75b61 100644 --- a/cache_internal_test.go +++ b/cache_internal_test.go @@ -9,11 +9,17 @@ import ( func TestDeletedCache(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + restore := func() { + nowFunc = time.Now + } + defer restore() nc := NewContext[string, int](ctx) key := "key" - nc.Set(key, 1, WithExpiration(-time.Second)) - + nc.Set(key, 1, WithExpiration(1*time.Second)) + nowFunc = func() time.Time { + return time.Now().Add(2 * time.Second) + } _, ok := nc.cache.Get(key) if !ok { t.Fatal("want true")