Make caches singletons

This commit is contained in:
Deluan
2020-10-25 22:49:37 -04:00
parent 1e56f4da76
commit 81d7556cdf
4 changed files with 46 additions and 30 deletions
+19 -10
View File
@@ -12,6 +12,7 @@ import (
"io"
"os"
"strings"
"sync"
"time"
"github.com/deluan/navidrome/core/cache"
@@ -205,15 +206,23 @@ func readFromFile(path string) ([]byte, error) {
return buf.Bytes(), nil
}
var (
onceImageCache sync.Once
imageCache ArtworkCache
)
func NewImageCache() ArtworkCache {
return cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems,
func(ctx context.Context, arg cache.Item) (io.Reader, error) {
info := arg.(*imageInfo)
reader, err := info.a.getArtwork(ctx, info.id, info.path, info.size)
if err != nil {
log.Error(ctx, "Error loading artwork art", "path", info.path, "size", info.size, err)
return nil, err
}
return reader, nil
})
onceImageCache.Do(func() {
imageCache = cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems,
func(ctx context.Context, arg cache.Item) (io.Reader, error) {
info := arg.(*imageInfo)
reader, err := info.a.getArtwork(ctx, info.id, info.path, info.size)
if err != nil {
log.Error(ctx, "Error loading artwork art", "path", info.path, "size", info.size, err)
return nil, err
}
return reader, nil
})
})
return imageCache
}