feat: add AvgCacheState to track average cache hits and misses

This commit is contained in:
2025-01-21 20:02:34 -06:00
parent 5a9b07f609
commit 745ee37184
2 changed files with 68 additions and 3 deletions

View File

@@ -5,8 +5,10 @@ import (
"log"
"net/http"
"net/url"
"s1d3sw1ped/SteamCache2/steamcache/avgcachestate"
"s1d3sw1ped/SteamCache2/vfs"
"s1d3sw1ped/SteamCache2/vfs/cache"
"s1d3sw1ped/SteamCache2/vfs/cachestate"
"s1d3sw1ped/SteamCache2/vfs/disk"
"s1d3sw1ped/SteamCache2/vfs/gc"
"s1d3sw1ped/SteamCache2/vfs/memory"
@@ -25,6 +27,8 @@ type SteamCache struct {
memory *memory.MemoryFS
disk *disk.DiskFS
hits *avgcachestate.AvgCacheState
dirty bool
mu sync.Mutex
}
@@ -98,16 +102,16 @@ func (sc *SteamCache) LogStats() {
defer sc.mu.Unlock()
if sc.dirty {
log.Printf(
"SteamCache2 %s: (%d) %s/%s %s: (%d) %s/%s",
"SteamCache2 %s: (%d) %s/%s %s: (%d) %s/%s Hitrate: %f%%",
sc.memory.Name(), len(sc.memory.StatAll()), units.HumanSize(float64(sc.memory.Size())), units.HumanSize(float64(sc.memory.Capacity())),
sc.disk.Name(), len(sc.disk.StatAll()), units.HumanSize(float64(sc.disk.Size())), units.HumanSize(float64(sc.disk.Capacity())),
sc.hits.Avg()*100,
)
sc.dirty = false
}
}
func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Only GET method is supported", http.StatusMethodNotAllowed)
return
@@ -148,6 +152,7 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
data, err := sc.vfs.Get(cacheKey)
if err == nil {
sc.hits.Add(cachestate.CacheStateHit)
w.Header().Add("X-LanCache-Status", "HIT")
w.Write(data)
return
@@ -185,6 +190,7 @@ func (sc *SteamCache) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
sc.vfs.Set(cacheKey, body)
sc.hits.Add(cachestate.CacheStateMiss)
w.Header().Add("X-LanCache-Status", "MISS")
w.Write(body)
}
@@ -223,6 +229,5 @@ func forward(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Add("X-LanCache-Status", "MISS")
w.Write(body)
}