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

View file

@ -29,7 +29,7 @@ func CreateScanner(musicFolder string) scanner.Scanner {
dataStore := persistence.New() dataStore := persistence.New()
artworkCache := core.NewImageCache() artworkCache := core.NewImageCache()
artwork := core.NewArtwork(dataStore, artworkCache) artwork := core.NewArtwork(dataStore, artworkCache)
cacheWarmer := core.NewCacheWarmer(artworkCache, artwork) cacheWarmer := core.NewCacheWarmer(artwork)
scannerScanner := scanner.New(dataStore, cacheWarmer) scannerScanner := scanner.New(dataStore, cacheWarmer)
return scannerScanner return scannerScanner
} }

View file

@ -12,6 +12,7 @@ import (
"io" "io"
"os" "os"
"strings" "strings"
"sync"
"time" "time"
"github.com/deluan/navidrome/core/cache" "github.com/deluan/navidrome/core/cache"
@ -205,15 +206,23 @@ func readFromFile(path string) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
var (
onceImageCache sync.Once
imageCache ArtworkCache
)
func NewImageCache() ArtworkCache { func NewImageCache() ArtworkCache {
return cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems, onceImageCache.Do(func() {
func(ctx context.Context, arg cache.Item) (io.Reader, error) { imageCache = cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems,
info := arg.(*imageInfo) func(ctx context.Context, arg cache.Item) (io.Reader, error) {
reader, err := info.a.getArtwork(ctx, info.id, info.path, info.size) info := arg.(*imageInfo)
if err != nil { reader, err := info.a.getArtwork(ctx, info.id, info.path, info.size)
log.Error(ctx, "Error loading artwork art", "path", info.path, "size", info.size, err) if err != nil {
return nil, err log.Error(ctx, "Error loading artwork art", "path", info.path, "size", info.size, err)
} return nil, err
return reader, nil }
}) return reader, nil
})
})
return imageCache
} }

View file

@ -14,10 +14,9 @@ type CacheWarmer interface {
Flush(ctx context.Context) Flush(ctx context.Context)
} }
func NewCacheWarmer(cache ArtworkCache, artwork Artwork) CacheWarmer { func NewCacheWarmer(artwork Artwork) CacheWarmer {
w := &warmer{ w := &warmer{
artwork: artwork, artwork: artwork,
cache: cache,
albums: map[string]struct{}{}, albums: map[string]struct{}{},
} }
p, err := pool.NewPool("artwork", 3, &artworkItem{}, w.execute) p, err := pool.NewPool("artwork", 3, &artworkItem{}, w.execute)
@ -33,7 +32,6 @@ func NewCacheWarmer(cache ArtworkCache, artwork Artwork) CacheWarmer {
type warmer struct { type warmer struct {
pool *pool.Pool pool *pool.Pool
artwork Artwork artwork Artwork
cache ArtworkCache
albums map[string]struct{} albums map[string]struct{}
} }

View file

@ -6,6 +6,7 @@ import (
"io" "io"
"mime" "mime"
"os" "os"
"sync"
"time" "time"
"github.com/deluan/navidrome/conf" "github.com/deluan/navidrome/conf"
@ -167,21 +168,29 @@ func selectTranscodingOptions(ctx context.Context, ds model.DataStore, mf *model
return return
} }
var (
onceTranscodingCache sync.Once
transcodingCache TranscodingCache
)
func NewTranscodingCache() TranscodingCache { func NewTranscodingCache() TranscodingCache {
return cache.NewFileCache("Transcoding", conf.Server.TranscodingCacheSize, onceTranscodingCache.Do(func() {
consts.TranscodingCacheDir, consts.DefaultTranscodingCacheMaxItems, transcodingCache = cache.NewFileCache("Transcoding", conf.Server.TranscodingCacheSize,
func(ctx context.Context, arg cache.Item) (io.Reader, error) { consts.TranscodingCacheDir, consts.DefaultTranscodingCacheMaxItems,
job := arg.(*streamJob) func(ctx context.Context, arg cache.Item) (io.Reader, error) {
t, err := job.ms.ds.Transcoding(ctx).FindByFormat(job.format) job := arg.(*streamJob)
if err != nil { t, err := job.ms.ds.Transcoding(ctx).FindByFormat(job.format)
log.Error(ctx, "Error loading transcoding command", "format", job.format, err) if err != nil {
return nil, os.ErrInvalid log.Error(ctx, "Error loading transcoding command", "format", job.format, err)
} return nil, os.ErrInvalid
out, err := job.ms.ffm.Start(ctx, t.Command, job.mf.Path, job.bitRate) }
if err != nil { out, err := job.ms.ffm.Start(ctx, t.Command, job.mf.Path, job.bitRate)
log.Error(ctx, "Error starting transcoder", "id", job.mf.ID, err) if err != nil {
return nil, os.ErrInvalid log.Error(ctx, "Error starting transcoder", "id", job.mf.ID, err)
} return nil, os.ErrInvalid
return out, nil }
}) return out, nil
})
})
return transcodingCache
} }