Improve caching
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"bytes"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -54,3 +56,34 @@ func BenchmarkResizeBox(b *testing.B) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// After a restart (empty memory cache) the digest comes from the on-disk index
|
||||
// without reading the source file.
|
||||
func TestSourceDigestPersists(t *testing.T) {
|
||||
old := thumbCacheDir
|
||||
thumbCacheDir = t.TempDir()
|
||||
t.Cleanup(func() { thumbCacheDir = old })
|
||||
|
||||
src := filepath.Join(t.TempDir(), "a.jpg")
|
||||
if err := os.WriteFile(src, []byte("photo"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
info, _ := os.Stat(src)
|
||||
h1, data, err := sourceDigest(src, info)
|
||||
if err != nil || data == nil {
|
||||
t.Fatalf("first call: %v, data=%v", err, data)
|
||||
}
|
||||
|
||||
digestCacheMu.Lock()
|
||||
delete(digestCache, src)
|
||||
digestCacheMu.Unlock()
|
||||
// Removing the source proves the second call does not read it again.
|
||||
if err := os.Remove(src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h2, data, err := sourceDigest(src, info)
|
||||
if err != nil || data != nil || h2 != h1 {
|
||||
t.Fatalf("after restart: h=%s data=%v err=%v, want %s from index", h2, data, err, h1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user