From 93bd26f4a89de853872b7a9983b8e7a310bdd5e3 Mon Sep 17 00:00:00 2001 From: k1low Date: Tue, 5 Mar 2024 11:53:36 +0900 Subject: [PATCH] Add StopAdjust and StopAll --- diskcache.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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: + } } }