@@ -81,23 +81,25 @@ func New[K comparable, V any](size int, ttl time.Duration) *Sieve[K, V] {
81
81
}
82
82
83
83
if ttl != 0 {
84
- go func (ctx context.Context ) {
85
- ticker := time .NewTicker (ttl / numberOfBuckets )
86
- defer ticker .Stop ()
87
- for {
88
- select {
89
- case <- ctx .Done ():
90
- return
91
- case <- ticker .C :
92
- cache .deleteExpired ()
93
- }
94
- }
95
- }(cache .ctx )
84
+ go cache .cleanup (cache .ctx )
96
85
}
97
86
98
87
return cache
99
88
}
100
89
90
+ func (s * Sieve [K , V ]) cleanup (ctx context.Context ) {
91
+ ticker := time .NewTicker (s .ttl / numberOfBuckets )
92
+ defer ticker .Stop ()
93
+ for {
94
+ select {
95
+ case <- ctx .Done ():
96
+ return
97
+ case <- ticker .C :
98
+ s .deleteExpired ()
99
+ }
100
+ }
101
+ }
102
+
101
103
func (s * Sieve [K , V ]) Set (key K , value V ) {
102
104
s .mu .Lock ()
103
105
defer s .mu .Unlock ()
@@ -147,7 +149,7 @@ func (s *Sieve[K, V]) Remove(key K) (ok bool) {
147
149
s .hand = s .hand .Prev ()
148
150
}
149
151
150
- s .removeEntry (e )
152
+ s .removeEntry (e , types . EvictReasonRemoved )
151
153
return true
152
154
}
153
155
@@ -191,7 +193,7 @@ func (s *Sieve[K, V]) Purge() {
191
193
defer s .mu .Unlock ()
192
194
193
195
for _ , e := range s .items {
194
- s .removeEntry (e )
196
+ s .removeEntry (e , types . EvictReasonRemoved )
195
197
}
196
198
197
199
for i := range s .buckets {
@@ -213,9 +215,9 @@ func (s *Sieve[K, V]) Close() {
213
215
s .mu .Unlock ()
214
216
}
215
217
216
- func (s * Sieve [K , V ]) removeEntry (e * entry [K , V ]) {
218
+ func (s * Sieve [K , V ]) removeEntry (e * entry [K , V ], reason types. EvictReason ) {
217
219
if s .callback != nil {
218
- s .callback (e .key , e .value )
220
+ s .callback (e .key , e .value , reason )
219
221
}
220
222
221
223
s .ll .Remove (e .element )
@@ -249,7 +251,7 @@ func (s *Sieve[K, V]) evict() {
249
251
}
250
252
251
253
s .hand = o .Prev ()
252
- s .removeEntry (el )
254
+ s .removeEntry (el , types . EvictReasonEvicted )
253
255
}
254
256
255
257
func (s * Sieve [K , V ]) addToBucket (e * entry [K , V ]) {
@@ -285,7 +287,7 @@ func (s *Sieve[K, V]) deleteExpired() {
285
287
}
286
288
287
289
for _ , e := range bucket .entries {
288
- s .removeEntry (e )
290
+ s .removeEntry (e , types . EvictReasonExpired )
289
291
}
290
292
291
293
s .mu .Unlock ()
0 commit comments