From eefdce7b0357b13a83e7e761e87cea3c94ca845d Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 8 Oct 2021 21:26:02 +0000 Subject: [PATCH] Add cache purge (#7) Reviewed-on: https://git.jojodev.com/jolheiser/overlay/pulls/7 Co-authored-by: jolheiser Co-committed-by: jolheiser --- bench.txt | 9 --------- overlay.go | 14 +++++++++----- 2 files changed, 9 insertions(+), 14 deletions(-) delete mode 100644 bench.txt diff --git a/bench.txt b/bench.txt deleted file mode 100644 index ba8bf45..0000000 --- a/bench.txt +++ /dev/null @@ -1,9 +0,0 @@ -go test -benchmem -bench=. -goos: linux -goarch: amd64 -pkg: go.jolheiser.com/overlay -cpu: Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz -BenchmarkCache-8 134959974 9.003 ns/op 0 B/op 0 allocs/op -BenchmarkNoCache-8 897212 1369 ns/op 280 B/op 4 allocs/op -PASS -ok go.jolheiser.com/overlay 3.360s diff --git a/overlay.go b/overlay.go index 512bf1c..2c14fc3 100644 --- a/overlay.go +++ b/overlay.go @@ -18,12 +18,17 @@ type FS struct { cache map[string]bool } +// PurgeCache purges the cache +func (f *FS) PurgeCache() { + f.cache = make(map[string]bool) +} + func (f *FS) apn(name string) string { return path.Join(f.root, name) } func (f *FS) exists(name string) bool { - if has, ok := f.cache[name]; ok && f.doCache { + if has, ok := f.cache[name]; ok { return has } _, err := os.Stat(f.apn(name)) @@ -63,10 +68,9 @@ type Option func(*FS) error // New returns a new FS func New(root string, fs fs.FS, opts ...Option) (*FS, error) { x := &FS{ - fs: fs, - root: root, - doCache: true, - cache: make(map[string]bool), + fs: fs, + root: root, + cache: make(map[string]bool), } for _, opt := range opts {