@@ -72,6 +72,7 @@ type Cache[K Key, V any] struct {
7272 keyToHash func (K ) (uint64 , uint64 )
7373 // stop is used to stop the processItems goroutine.
7474 stop chan struct {}
75+ done chan struct {}
7576 // indicates whether cache is closed.
7677 isClosed atomic.Bool
7778 // cost calculates cost from a value.
@@ -227,6 +228,7 @@ func NewCache[K Key, V any](config *Config[K, V]) (*Cache[K, V], error) {
227228 setBuf : make (chan * Item [V ], setBufSize ),
228229 keyToHash : config .KeyToHash ,
229230 stop : make (chan struct {}),
231+ done : make (chan struct {}),
230232 cost : config .Cost ,
231233 ignoreInternalCost : config .IgnoreInternalCost ,
232234 cleanupTicker : time .NewTicker (time .Duration (config .TtlTickerDurationInSec ) * time .Second / 2 ),
@@ -422,7 +424,9 @@ func (c *Cache[K, V]) Close() {
422424
423425 // Block until processItems goroutine is returned.
424426 c .stop <- struct {}{}
427+ <- c .done
425428 close (c .stop )
429+ close (c .done )
426430 close (c .setBuf )
427431 c .cachePolicy .Close ()
428432 c .cleanupTicker .Stop ()
@@ -438,6 +442,7 @@ func (c *Cache[K, V]) Clear() {
438442 }
439443 // Block until processItems goroutine is returned.
440444 c .stop <- struct {}{}
445+ <- c .done
441446
442447 // Clear out the setBuf channel.
443448loop:
@@ -556,6 +561,7 @@ func (c *Cache[K, V]) processItems() {
556561 case <- c .cleanupTicker .C :
557562 c .storedItems .Cleanup (c .cachePolicy , onEvict )
558563 case <- c .stop :
564+ c .done <- struct {}{}
559565 return
560566 }
561567 }
0 commit comments