Added CleanCache() method to the solver that deletes all the caches if the total size grows above a certain (configurable) limit (default: 500 MiB). The function is called externally to handle errors (usually log or ignore completely) and to avoid calling multiple times for multiple depsolves of a single request. The cleanup is extremely simple and is meant as a placeholder for more sophisticated cache management. The goal is to simply avoid ballooning cache sizes that might cause issues for users or our own services.
31 lines
523 B
Go
31 lines
523 B
Go
package dnfjson
|
|
|
|
import (
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func dirSize(path string) (uint64, error) {
|
|
var size uint64
|
|
sizer := func(path string, info fs.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
size += uint64(info.Size())
|
|
return nil
|
|
}
|
|
err := filepath.Walk(path, sizer)
|
|
return size, err
|
|
}
|
|
|
|
func (bs *BaseSolver) CleanCache() error {
|
|
curSize, err := dirSize(bs.cacheDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if curSize > bs.maxCacheSize {
|
|
return os.RemoveAll(bs.cacheDir)
|
|
}
|
|
return nil
|
|
}
|