Refactor to serve all responses from cached files on disk

- StoreOutput now returns output hash for immediate retrieval
- Cache misses now serve from disk file after storing (same as hits)
- Log served_bytes from actual io.Copy result (avoids stat calls)
- Remove ContentLength field usage for cache hits (stream from file)
- Fix tests to properly check all return values
This commit is contained in:
2026-01-08 05:11:55 -08:00
parent 1a97f42cd8
commit 6304556837
4 changed files with 60 additions and 45 deletions

View File

@@ -124,26 +124,26 @@ func (s *Handlers) HandleImage() http.HandlerFunc {
w.Header().Set("ETag", resp.ETag)
}
// Log cache status and timing
// Stream the response
w.WriteHeader(http.StatusOK)
servedBytes, err := io.Copy(w, resp.Content)
if err != nil {
s.log.Error("failed to write response",
"error", err,
)
}
// Log cache status and timing after serving
duration := time.Since(startTime)
s.log.Info("image served",
"cache_key", cacheKey,
"cache_status", resp.CacheStatus,
"duration_ms", duration.Milliseconds(),
"format", req.Format,
"served_bytes", resp.ContentLength,
"served_bytes", servedBytes,
"fetched_bytes", resp.FetchedBytes,
)
// Stream the response
w.WriteHeader(http.StatusOK)
_, err = io.Copy(w, resp.Content)
if err != nil {
s.log.Error("failed to write response",
"error", err,
)
}
}
}