Keep cached sources and variants in one FileStore type (closes #318)
check / check (push) Canceled after 0s

imgcache.FileStore is a directory of files named by a hex key, at
<ab>/<cd>/<key>. It writes a file under a temp name, syncs it and renames
it into place. It opens, reports the size of and deletes the file for a
key. The cache keeps its sources in one FileStore and its variants in
another, so ContentStorage and VariantStorage are gone. FileStore has no
Exists: the reconciliation pass checks a variant's file with Size, and the
tests look for the file on disk. Cache.Variant reads the content type from
the file's first bytes itself. storage.go is now filestore.go, and the
unused testdata/red.avif is gone.

Model: opus-5-5
This commit is contained in:
2026-10-26 19:13:04 +00:00
parent 17376d885b
commit e0539a8601
12 changed files with 496 additions and 535 deletions
+6 -5
View File
@@ -48,7 +48,7 @@ const staleTempFileAge = time.Hour
const sqliteTimestampLayout = "2006-01-02 15:04:05"
// tempFilePrefix is the prefix os.CreateTemp uses for in-flight cache
// writes (".tmp-*" patterns in the storage layer).
// writes (the ".tmp-*" pattern in writeFileSynced).
const tempFilePrefix = ".tmp-"
// UsageBytes returns the total number of bytes of cache content
@@ -306,7 +306,7 @@ func (c *Cache) evictVariant(
return fmt.Errorf("failed to delete variant accounting row: %w", err)
}
err = c.variants.Delete(cacheKey)
err = c.variants.Delete(string(cacheKey))
if err != nil {
return err
}
@@ -325,7 +325,7 @@ func (c *Cache) evictSourceBlob(ctx context.Context, key imagerequest.SourceKey)
return fmt.Errorf("failed to delete source content row: %w", err)
}
return c.srcContent.Delete(key)
return c.srcContent.Delete(string(key))
}
// notifyWritePressure wakes the background evictor after a store, so
@@ -636,11 +636,12 @@ func (c *Cache) reconcileVariantRows(ctx context.Context) error {
return ctx.Err()
}
if c.variants.Exists(key) {
_, err := c.variants.Size(string(key))
if err == nil {
continue
}
_, err := c.db.ExecContext(ctx,
_, err = c.db.ExecContext(ctx,
`DELETE FROM variant_content WHERE cache_key = ?`, string(key))
if err != nil {
return fmt.Errorf("failed to drop stale variant accounting row: %w", err)