All checks were successful
Release / Build versioned release (push) Successful in 20s
76 lines
1.7 KiB
Go
76 lines
1.7 KiB
Go
package steamcache
|
|
|
|
import (
|
|
"s1d3sw1ped/SteamCache2/steamcache/logger"
|
|
"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
|
|
// logger.Logger.Error().Err(err).Msgf("Failed to delete %s", randfile.Name())
|
|
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
|
|
}
|
|
}
|
|
|
|
logger.Logger.Info().
|
|
Str("name", vfss.Name()).
|
|
Str("duration", time.Since(tstart).String()).
|
|
Str("reclaimed", units.HumanSize(float64(reclaimed))).
|
|
Int("deletions", deletions).
|
|
Msgf("GC")
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
logger.Logger.Info().
|
|
Str("name", vfss.Name()).
|
|
Str("duration", time.Since(tstart).String()).
|
|
Str("reclaimed", units.HumanSize(float64(reclaimed))).
|
|
Int("deletions", deletions).
|
|
Msgf("GC")
|
|
}
|
|
|
|
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
|
|
}
|