Fix pruning on DiskCache object creation

This commit is contained in:
Kovid Goyal
2025-09-30 13:18:00 +05:30
parent 8fdbf8d7da
commit 712faa7aa5
2 changed files with 26 additions and 11 deletions

View File

@@ -22,18 +22,27 @@ func new_disk_cache(path string, max_size int64) (dc *DiskCache, err error) {
if err = os.MkdirAll(path, 0o700); err != nil { if err = os.MkdirAll(path, 0o700); err != nil {
return return
} }
dc = &DiskCache{Path: path, MaxSize: max_size} ans := &DiskCache{Path: path, MaxSize: max_size}
dc.lock() ans.lock()
defer dc.unlock() defer ans.unlock()
if err = dc.prune(); err != nil { if err = ans.ensure_entries(); err != nil {
return return
} }
if dc.get_dir, err = os.MkdirTemp(dc.Path, "getdir-*"); err != nil { if pruned, err := ans.prune(); err != nil {
return nil, err
} else if pruned {
if err = ans.write_entries(); err != nil {
return nil, err
}
}
if ans.get_dir, err = os.MkdirTemp(ans.Path, "getdir-*"); err != nil {
return return
} }
err = utils.AtExitRmtree(dc.get_dir) if err = utils.AtExitRmtree(ans.get_dir); err != nil {
return return
} }
return ans, nil
}
func key_for_path(path string) (key string, err error) { func key_for_path(path string) (key string, err error) {
if path, err = filepath.EvalSymlinks(path); err != nil { if path, err = filepath.EvalSymlinks(path); err != nil {
@@ -211,9 +220,9 @@ func (dc *DiskCache) remove(key string) (err error) {
return return
} }
func (dc *DiskCache) prune() error { func (dc *DiskCache) prune() (bool, error) {
if dc.MaxSize < 1 || dc.entries.TotalSize <= dc.MaxSize { if dc.MaxSize < 1 || dc.entries.TotalSize <= dc.MaxSize {
return nil return false, nil
} }
for dc.entries.TotalSize > dc.MaxSize && len(dc.entries.SortedEntries) > 0 { for dc.entries.TotalSize > dc.MaxSize && len(dc.entries.SortedEntries) > 0 {
base := dc.folder_for_key(dc.entries.SortedEntries[0].Key) base := dc.folder_for_key(dc.entries.SortedEntries[0].Key)
@@ -223,10 +232,10 @@ func (dc *DiskCache) prune() error {
dc.entries.TotalSize = max(0, dc.entries.TotalSize-t.Size) dc.entries.TotalSize = max(0, dc.entries.TotalSize-t.Size)
dc.entries.SortedEntries = dc.entries.SortedEntries[1:] dc.entries.SortedEntries = dc.entries.SortedEntries[1:]
} else { } else {
return err return false, err
} }
} }
return nil return true, nil
} }
func (dc *DiskCache) update_timestamp(key string) { func (dc *DiskCache) update_timestamp(key string) {

View File

@@ -69,4 +69,10 @@ func TestDiskCache(t *testing.T) {
ak("k1", "k2") ak("k1", "k2")
dc.Add("k3", map[string][]byte{"1": []byte(strings.Repeat("a", int(dc.MaxSize)-10))}) dc.Add("k3", map[string][]byte{"1": []byte(strings.Repeat("a", int(dc.MaxSize)-10))})
ak("k3", "k2") ak("k3", "k2")
// check that creating a new disk cache prunes
_, err = NewDiskCache(tdir, dc.MaxSize-8)
if err != nil {
t.Fatal(err)
}
ak("k3")
} }