Skip to content

Commit dfc697b

Browse files
committed
fixed to call janitor run method
1 parent 52983e9 commit dfc697b

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

cache.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import (
1616

1717
// Interface is a common-cache interface.
1818
type Interface[K comparable, V any] interface {
19+
// Get looks up a key's value from the cache.
1920
Get(key K) (value V, ok bool)
21+
// Set sets a value to the cache with key. replacing any existing value.
2022
Set(key K, val V)
23+
// Keys returns the keys of the cache. The order is relied on algorithms.
2124
Keys() []K
25+
// Delete deletes the item with provided key from the cache.
2226
Delete(key K)
2327
}
2428

@@ -152,15 +156,8 @@ func WithJanitorInterval[K comparable, V any](d time.Duration) Option[K, V] {
152156
//
153157
// There are several Cache replacement policies available with you specified any options.
154158
func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
155-
o := newOptions[K, V]()
156-
for _, optFunc := range opts {
157-
optFunc(o)
158-
}
159159
ctx, cancel := context.WithCancel(context.Background())
160-
cache := &Cache[K, V]{
161-
cache: o.cache,
162-
janitor: newJanitor(ctx, o.janitorInterval),
163-
}
160+
cache := NewContext[K, V](ctx)
164161
runtime.SetFinalizer(cache, func(self *Cache[K, V]) {
165162
cancel()
166163
})
@@ -175,10 +172,12 @@ func NewContext[K comparable, V any](ctx context.Context, opts ...Option[K, V])
175172
for _, optFunc := range opts {
176173
optFunc(o)
177174
}
178-
return &Cache[K, V]{
175+
cache := &Cache[K, V]{
179176
cache: o.cache,
180177
janitor: newJanitor(ctx, o.janitorInterval),
181178
}
179+
cache.janitor.run(cache.DeleteExpired)
180+
return cache
182181
}
183182

184183
// Get looks up a key's value from the cache.

0 commit comments

Comments
 (0)