@@ -16,9 +16,13 @@ import (
16
16
17
17
// Interface is a common-cache interface.
18
18
type Interface [K comparable , V any ] interface {
19
+ // Get looks up a key's value from the cache.
19
20
Get (key K ) (value V , ok bool )
21
+ // Set sets a value to the cache with key. replacing any existing value.
20
22
Set (key K , val V )
23
+ // Keys returns the keys of the cache. The order is relied on algorithms.
21
24
Keys () []K
25
+ // Delete deletes the item with provided key from the cache.
22
26
Delete (key K )
23
27
}
24
28
@@ -152,15 +156,8 @@ func WithJanitorInterval[K comparable, V any](d time.Duration) Option[K, V] {
152
156
//
153
157
// There are several Cache replacement policies available with you specified any options.
154
158
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
- }
159
159
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 )
164
161
runtime .SetFinalizer (cache , func (self * Cache [K , V ]) {
165
162
cancel ()
166
163
})
@@ -175,10 +172,12 @@ func NewContext[K comparable, V any](ctx context.Context, opts ...Option[K, V])
175
172
for _ , optFunc := range opts {
176
173
optFunc (o )
177
174
}
178
- return & Cache [K , V ]{
175
+ cache := & Cache [K , V ]{
179
176
cache : o .cache ,
180
177
janitor : newJanitor (ctx , o .janitorInterval ),
181
178
}
179
+ cache .janitor .run (cache .DeleteExpired )
180
+ return cache
182
181
}
183
182
184
183
// Get looks up a key's value from the cache.
0 commit comments