Skip to content

Commit

Permalink
Merge pull request #66 from 2manymws/revert-64-rm-empty-dir
Browse files Browse the repository at this point in the history
Revert "Remove directories recursively when it is zero cache files."
  • Loading branch information
k1LoW authored Jul 19, 2024
2 parents 5d2d94a + 46bb601 commit 715eb19
Showing 1 changed file with 2 additions and 31 deletions.
33 changes: 2 additions & 31 deletions diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,8 @@ func (c *DiskCache) removeCache(ci *cacheItem) {
defer func() {
c.d.remove(ci.key)
}()
_ = os.Remove(ci.pathkey + reqCacheSuffix) //nostyle:handlerrors
_ = os.Remove(ci.pathkey + resCacheSuffix) //nostyle:handlerrors
_ = c.recursiveRemoveDir(filepath.Dir(ci.pathkey)) //nostyle:handlerrors

_ = os.Remove(ci.pathkey + reqCacheSuffix) //nostyle:handlerrors
_ = os.Remove(ci.pathkey + resCacheSuffix) //nostyle:handlerrors
c.mu.Lock()
defer c.mu.Unlock()
if c.totalBytes < ci.bytes {
Expand All @@ -545,33 +543,6 @@ func (c *DiskCache) removeCache(ci *cacheItem) {
}
}

func (c *DiskCache) recursiveRemoveDir(dir string) error {
if c.cacheRoot == dir {
return nil
}
entries, err := os.ReadDir(dir)
if err != nil {
return err
}
found := false
for _, e := range entries {
if e.IsDir() {
continue
}
if strings.HasSuffix(e.Name(), reqCacheSuffix) || strings.HasSuffix(e.Name(), resCacheSuffix) {
found = true
break
}
}
if found {
return nil
}
if err := os.RemoveAll(dir); err != nil {
return err
}
return c.recursiveRemoveDir(filepath.Dir(dir))
}

func isWritable(dir string) (bool, error) {
const tmpFile = "tmpfile"
file, err := os.CreateTemp(dir, tmpFile)
Expand Down

0 comments on commit 715eb19

Please sign in to comment.