Skip to content

feature: allow vacuum disablement #22

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ actual cache time will always be lower or equal to this.
*Default: 600*

The number of seconds to wait between cache cleanup runs.

A value of -1 disables cleanup. It means stale records won't be cleaned up so if you
don't limit yourself to a fixed set of paths that is managed by this plugin you risk
filling up your cache filesystem. But it avoids issue [#21](https://github.com/traefik/plugin-simplecache/issues/21)

#### Add Status Header (`addStatusHeader`)

Expand Down
6 changes: 4 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"time"
Expand Down Expand Up @@ -34,6 +35,7 @@ const (
cacheHitStatus = "hit"
cacheMissStatus = "miss"
cacheErrorStatus = "error"
cleanupDisabled = -1
)

type cache struct {
Expand All @@ -49,8 +51,8 @@ func New(_ context.Context, next http.Handler, cfg *Config, name string) (http.H
return nil, errors.New("maxExpiry must be greater or equal to 1")
}

if cfg.Cleanup <= 1 {
return nil, errors.New("cleanup must be greater or equal to 1")
if cfg.Cleanup <= 1 && cfg.Cleanup != cleanupDisabled {
return nil, fmt.Errorf("cleanup must be greater or equal to 1 or disabled %d", cleanupDisabled)
}

fc, err := newFileCache(cfg.Path, time.Duration(cfg.Cleanup)*time.Second)
Expand Down
10 changes: 9 additions & 1 deletion cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ func TestNew(t *testing.T) {
wantErr: true,
},
{
name: "should error if cleanup <= 1",
name: "should error if cleanup <= 1 and not -1",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 300, Cleanup: 1},
wantErr: true,
},
{
name: "should not error if cleanup == -1",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 300, Cleanup: -1},
wantErr: false,
},
{
name: "should be valid",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 300, Cleanup: 600},
Expand All @@ -44,6 +49,9 @@ func TestNew(t *testing.T) {
if test.wantErr && err == nil {
t.Fatal("expected error on bad regexp format")
}
if !test.wantErr && err != nil {
t.Fatalf("did not expect error but got %s", err)
}
})
}
}
Expand Down
4 changes: 3 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func newFileCache(path string, vacuum time.Duration) (*fileCache, error) {
pm: &pathMutex{lock: map[string]*fileLock{}},
}

go fc.vacuum(vacuum)
if vacuum > 0 * time.Second {
go fc.vacuum(vacuum)
}

return fc, nil
}
Expand Down