Skip to content

Commit

Permalink
Merge pull request #55 from 2manymws/stop-adjust
Browse files Browse the repository at this point in the history
Add StopAdjust and StopAll
  • Loading branch information
k1LoW authored Mar 5, 2024
2 parents d945825 + 93bd26f commit c644371
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -393,6 +405,11 @@ func (c *DiskCache) removeCachesUntilAdjustTotalBytes() {
return
}
c.mu.Unlock()
select {
case <-c.adjustStopCh:
return
default:
}
}
}

Expand Down

0 comments on commit c644371

Please sign in to comment.