Skip to content

Commit 9790bda

Browse files
committed
fixed New function
1 parent 72cfa42 commit 9790bda

File tree

2 files changed

+3
-36
lines changed

2 files changed

+3
-36
lines changed

cache.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cache
22

33
import (
44
"context"
5-
"runtime"
65
"sync"
76
"time"
87

@@ -151,20 +150,15 @@ func WithJanitorInterval[K comparable, V any](d time.Duration) Option[K, V] {
151150
}
152151

153152
// New creates a new thread safe Cache.
154-
// This function will be stopped an internal janitor when the cache is
155-
// no longer referenced anywhere.
153+
// The janitor will not be stopped which is created by this function.
156154
//
157155
// There are several Cache replacement policies available with you specified any options.
158156
func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
159-
ctx, cancel := context.WithCancel(context.Background())
160-
cache := NewContext(ctx, opts...)
161-
runtime.SetFinalizer(cache, func(self *Cache[K, V]) {
162-
cancel()
163-
})
164-
return cache
157+
return NewContext(context.Background(), opts...)
165158
}
166159

167160
// NewContext creates a new thread safe Cache with context.
161+
// This function will be stopped an internal janitor when the context is cancelled.
168162
//
169163
// There are several Cache replacement policies available with you specified any options.
170164
func NewContext[K comparable, V any](ctx context.Context, opts ...Option[K, V]) *Cache[K, V] {

cache_internal_test.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cache
22

33
import (
44
"context"
5-
"runtime"
65
"testing"
76
"time"
87
)
@@ -27,29 +26,3 @@ func TestDeletedCache(t *testing.T) {
2726
t.Fatal("want false")
2827
}
2928
}
30-
31-
func TestFinalizeCache(t *testing.T) {
32-
if runtime.GOARCH != "amd64" {
33-
t.Skipf("Skipping on non-amd64 machine")
34-
}
35-
36-
done := make(chan struct{})
37-
wait := make(chan struct{})
38-
go func() {
39-
c := New[string, int]()
40-
c.janitor.run(func() {
41-
close(done)
42-
})
43-
c = nil
44-
close(wait)
45-
}()
46-
<-wait
47-
48-
runtime.GC()
49-
50-
select {
51-
case <-done:
52-
case <-time.After(10 * time.Second):
53-
t.Fatal("expected to call a function which is set as finalizer")
54-
}
55-
}

0 commit comments

Comments
 (0)