Skip to content

Commit eb23746

Browse files
committed
Removed os.Stat caching
The underlying OS is much better at this.
1 parent 639661f commit eb23746

File tree

2 files changed

+4
-28
lines changed

2 files changed

+4
-28
lines changed

json.go

-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,5 @@ func (p *Path) UnmarshalJSON(b []byte) error {
4545
return err
4646
}
4747
(*p).path = s
48-
(*p).cachedFileInfo = nil
4948
return nil
5049
}

paths.go

+4-27
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ import (
4040

4141
// Path represents a path
4242
type Path struct {
43-
path string
44-
cachedFileInfo os.FileInfo
45-
cachedFileInfoTime time.Time
43+
path string
4644
}
4745

4846
// New creates a new Path object. If path is the empty string
@@ -64,30 +62,11 @@ func NewFromFile(file *os.File) *Path {
6462
return New(file.Name())
6563
}
6664

67-
func (p *Path) setCachedFileInfo(info os.FileInfo) {
68-
p.cachedFileInfo = info
69-
p.cachedFileInfoTime = time.Now()
70-
}
71-
7265
// Stat returns a FileInfo describing the named file. The result is
7366
// cached internally for next queries. To ensure that the cached
7467
// FileInfo entry is updated just call Stat again.
7568
func (p *Path) Stat() (os.FileInfo, error) {
76-
info, err := os.Stat(p.path)
77-
if err != nil {
78-
return nil, err
79-
}
80-
p.setCachedFileInfo(info)
81-
return info, nil
82-
}
83-
84-
func (p *Path) stat() (os.FileInfo, error) {
85-
if p.cachedFileInfo != nil {
86-
if p.cachedFileInfoTime.Add(50 * time.Millisecond).After(time.Now()) {
87-
return p.cachedFileInfo, nil
88-
}
89-
}
90-
return p.Stat()
69+
return os.Stat(p.path)
9170
}
9271

9372
// Clone create a copy of the Path object
@@ -226,13 +205,12 @@ func (p *Path) FollowSymLink() error {
226205
return err
227206
}
228207
p.path = resolvedPath
229-
p.cachedFileInfo = nil
230208
return nil
231209
}
232210

233211
// Exist return true if the path exists
234212
func (p *Path) Exist() (bool, error) {
235-
_, err := p.stat()
213+
_, err := p.Stat()
236214
if err == nil {
237215
return true, nil
238216
}
@@ -244,7 +222,7 @@ func (p *Path) Exist() (bool, error) {
244222

245223
// IsDir return true if the path exists and is a directory
246224
func (p *Path) IsDir() (bool, error) {
247-
info, err := p.stat()
225+
info, err := p.Stat()
248226
if err == nil {
249227
return info.IsDir(), nil
250228
}
@@ -264,7 +242,6 @@ func (p *Path) ReadDir() (PathList, error) {
264242
paths := PathList{}
265243
for _, info := range infos {
266244
path := p.Clone().Join(info.Name())
267-
path.setCachedFileInfo(info)
268245
paths.Add(path)
269246
}
270247
return paths, nil

0 commit comments

Comments
 (0)