diff --git a/diskcache.go b/diskcache.go index bf5db76..b0aec4a 100644 --- a/diskcache.go +++ b/diskcache.go @@ -91,6 +91,7 @@ type DiskCache struct { mu sync.Mutex keyMu *keyrwmutex.KeyRWMutex adjustMu sync.Mutex + adjustStopCh chan struct{} } // DiskCacheOption is an option for DiskCache. @@ -253,6 +254,12 @@ func NewDiskCache(cacheRoot string, defaultTTL time.Duration, opts ...DiskCacheO return c, nil } +// StopAll stops all the goroutines of the cache. +func (c *DiskCache) StopAll() { + c.StartAutoCleanup() + c.StopAdjust() +} + // StartAutoCleanup starts the goroutine of automatic cache cleanup func (c *DiskCache) StartAutoCleanup() { go c.m.Start() @@ -263,6 +270,11 @@ func (c *DiskCache) StopAutoCleanup() { c.m.Stop() } +// StopAdjust +func (c *DiskCache) StopAdjust() { + c.adjustStopCh <- struct{}{} +} + // DeleteExpired deletes expired caches. func (c *DiskCache) DeleteExpired() { c.m.DeleteExpired() @@ -393,6 +405,11 @@ func (c *DiskCache) removeCachesUntilAdjustTotalBytes() { return } c.mu.Unlock() + select { + case <-c.adjustStopCh: + return + default: + } } }