Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add StopAdjust and StopAll #55

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
mu sync.Mutex
keyMu *keyrwmutex.KeyRWMutex
adjustMu sync.Mutex
adjustStopCh chan struct{}
}

// DiskCacheOption is an option for DiskCache.
Expand Down Expand Up @@ -240,7 +241,7 @@
c.mu.Lock()
defer c.mu.Unlock()
c.totalBytes += size
c.m.Set(key, &cacheItem{

Check failure on line 244 in diskcache.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.funcfmt] Function and method calls should not be separated based solely on line length. (ref: https://google.github.io/styleguide/go/decisions#function-formatting )
path: path,
bytes: size,
}, ttlcache.DefaultTTL)
Expand All @@ -253,6 +254,12 @@
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 @@
c.m.Stop()
}

// StopAdjust
func (c *DiskCache) StopAdjust() {
c.adjustStopCh <- struct{}{}
}

// DeleteExpired deletes expired caches.
func (c *DiskCache) DeleteExpired() {
c.m.DeleteExpired()
Expand All @@ -278,7 +290,7 @@
func (c *DiskCache) StoreWithTTL(key string, req *http.Request, res *http.Response, ttl time.Duration) error {
c.keyMu.LockKey(key)
defer func() {
_ = c.keyMu.UnlockKey(key)

Check failure on line 293 in diskcache.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.handlerrors] Do not discard errors using `_` variables. If a function returns an error, check it to make sure the function succeeded. Handle the error, return it, or, in truly exceptional situations, panic. (ref: https://go.dev/wiki/CodeReviewComments#handle-errors )
}()
p := filepath.Join(c.cacheRoot, KeyToPath(key, c.cacheDirLen))
dir := filepath.Dir(p)
Expand Down Expand Up @@ -332,7 +344,7 @@
func (c *DiskCache) Load(key string) (*http.Request, *http.Response, error) {
c.keyMu.RLockKey(key)
defer func() {
_ = c.keyMu.RUnlockKey(key)

Check failure on line 347 in diskcache.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.handlerrors] Do not discard errors using `_` variables. If a function returns an error, check it to make sure the function succeeded. Handle the error, return it, or, in truly exceptional situations, panic. (ref: https://go.dev/wiki/CodeReviewComments#handle-errors )
}()
i := c.m.Get(key)
if i == nil {
Expand Down Expand Up @@ -393,6 +405,11 @@
return
}
c.mu.Unlock()
select {
case <-c.adjustStopCh:
return
default:
}
}
}

Expand All @@ -400,7 +417,7 @@
defer func() {
c.d.remove(ci.key)
}()
_ = os.Remove(ci.path)

Check failure on line 420 in diskcache.go

View workflow job for this annotation

GitHub Actions / Test

[gostyle.handlerrors] Do not discard errors using `_` variables. If a function returns an error, check it to make sure the function succeeded. Handle the error, return it, or, in truly exceptional situations, panic. (ref: https://go.dev/wiki/CodeReviewComments#handle-errors )
c.mu.Lock()
defer c.mu.Unlock()
if c.totalBytes < ci.bytes {
Expand Down
Loading