Add cache purge
continuous-integration/woodpecker the build was successful
Details
continuous-integration/woodpecker the build was successful
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>
parent
99bf189237
commit
b1161035be
|
@ -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
|
18
overlay.go
18
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))
|
||||
|
@ -65,7 +70,6 @@ func New(root string, fs fs.FS, opts ...Option) (*FS, error) {
|
|||
x := &FS{
|
||||
fs: fs,
|
||||
root: root,
|
||||
doCache: true,
|
||||
cache: make(map[string]bool),
|
||||
}
|
||||
|
||||
|
@ -94,13 +98,3 @@ func WithSub(sub string) Option {
|
|||
return
|
||||
}
|
||||
}
|
||||
|
||||
// WithCaching sets a caching mode for an FS
|
||||
// Caching avoids subsequent os.Stat to determine if a file exists on disk
|
||||
// See bench.txt for differences in usage
|
||||
func WithCaching(doCache bool) Option {
|
||||
return func(x *FS) error {
|
||||
x.doCache = doCache
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue