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

@@ -231,18 +231,18 @@ func (c *Cache) StoreSource(
return contentHash, nil
}
// StoreOutput stores processed output content.
// StoreOutput stores processed output content and returns the output hash.
func (c *Cache) StoreOutput(
ctx context.Context,
req *ImageRequest,
sourceMetadataID int64,
content io.Reader,
contentType string,
) error {
) (string, error) {
// Store content
outputHash, size, err := c.dstContent.Store(content)
if err != nil {
return fmt.Errorf("failed to store output content: %w", err)
return "", fmt.Errorf("failed to store output content: %w", err)
}
cacheKey := CacheKey(req)
@@ -254,7 +254,7 @@ func (c *Cache) StoreOutput(
ON CONFLICT(content_hash) DO NOTHING
`, outputHash, contentType, size)
if err != nil {
return fmt.Errorf("failed to insert output content: %w", err)
return "", fmt.Errorf("failed to insert output content: %w", err)
}
_, err = c.db.ExecContext(ctx, `
@@ -266,7 +266,7 @@ func (c *Cache) StoreOutput(
access_count = request_cache.access_count + 1
`, cacheKey, sourceMetadataID, outputHash, req.Size.Width, req.Size.Height, req.Format, req.Quality, req.FitMode)
if err != nil {
return fmt.Errorf("failed to insert request cache: %w", err)
return "", fmt.Errorf("failed to insert request cache: %w", err)
}
// Update hot cache
@@ -276,7 +276,7 @@ func (c *Cache) StoreOutput(
c.hotCacheMu.Unlock()
}
return nil
return outputHash, nil
}
// StoreNegative stores a negative cache entry for a failed fetch.