Files
steamcache2/steamcache/gc.go
2025-01-22 17:49:22 -06:00

66 lines
1.5 KiB
Go

package steamcache
import (
"log"
"s1d3sw1ped/SteamCache2/vfs"
"s1d3sw1ped/SteamCache2/vfs/cachestate"
"time"
"github.com/docker/go-units"
"golang.org/x/exp/rand"
)
func randomgc(vfss vfs.VFS, stats []*vfs.FileInfo) int64 {
// Pick a random file to delete
randfile := stats[rand.Intn(len(stats))]
sz := randfile.Size()
err := vfss.Delete(randfile.Name())
if err != nil {
// If we failed to delete the file, log it and return 0
// log.Printf("Failed to delete %s: %v", randfile.Name(), err)
return 0
}
return sz
}
func memorygc(vfss vfs.VFS, size int) {
tstart := time.Now()
deletions := 0
targetreclaim := int64(size)
var reclaimed int64
stats := vfss.StatAll()
for {
reclaimed += randomgc(vfss, stats)
deletions++
if reclaimed >= targetreclaim {
break
}
}
log.Printf("GC of %s took %v to reclaim %s by deleting %d files", vfss.Name(), time.Since(tstart), units.HumanSize(float64(reclaimed)), deletions)
}
func diskgc(vfss vfs.VFS, size int) {
tstart := time.Now()
deletions := 0
targetreclaim := int64(size)
var reclaimed int64
stats := vfss.StatAll()
for {
reclaimed += randomgc(vfss, stats)
deletions++
if reclaimed >= targetreclaim {
break
}
}
log.Printf("GC of %s took %v to reclaim %s by deleting %d files", vfss.Name(), time.Since(tstart), units.HumanSize(float64(reclaimed)), deletions)
}
func cachehandler(fi *vfs.FileInfo, cs cachestate.CacheState) bool {
return time.Since(fi.AccessTime()) < time.Minute*10 // Put files in the cache if they've been accessed twice in the last 10 minutes
}