🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Add dedicated Item interface for cache items
Deluan197d4024f738
diff --git a/core/artwork.go b/core/artwork.goindex b92cd9651..8c89b3dd2 100644--- a/core/artwork.go+++ b/core/artwork.go@@ -43,14 +43,14 @@ type artwork struct {}type imageInfo struct {- c *artwork+ a *artworkid stringpath stringsize intlastUpdate time.Time}-func (ci *imageInfo) String() string {+func (ci *imageInfo) Key() string {return fmt.Sprintf("%s.%d.%s.%d", ci.path, ci.size, ci.lastUpdate.Format(time.RFC3339Nano), conf.Server.CoverJpegQuality)}@@ -61,7 +61,7 @@ func (a *artwork) Get(ctx context.Context, id string, size int, out io.Writer) e}info := &imageInfo{- c: a,+ a: a,id: id,path: path,size: size,@@ -207,9 +207,9 @@ func readFromFile(path string) ([]byte, error) {func NewImageCache() ArtworkCache {return cache.NewFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems,- func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {+ func(ctx context.Context, arg cache.Item) (io.Reader, error) {info := arg.(*imageInfo)- reader, err := info.c.getArtwork(ctx, info.id, info.path, info.size)+ 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, errdiff --git a/core/cache/file_caches.go b/core/cache/file_caches.goindex 127ee80b0..b9eb78eda 100644--- a/core/cache/file_caches.go+++ b/core/cache/file_caches.go@@ -15,10 +15,14 @@ import ("github.com/dustin/go-humanize")-type ReadFunc func(ctx context.Context, arg fmt.Stringer) (io.Reader, error)+type Item interface {+ Key() string+}++type ReadFunc func(ctx context.Context, item Item) (io.Reader, error)type FileCache interface {- Get(ctx context.Context, arg fmt.Stringer) (*CachedStream, error)+ Get(ctx context.Context, item Item) (*CachedStream, error)Ready() bool}@@ -78,7 +82,7 @@ func (fc *fileCache) available(ctx context.Context) bool {return fc.ready && !fc.disabled}-func (fc *fileCache) Get(ctx context.Context, arg fmt.Stringer) (*CachedStream, error) {+func (fc *fileCache) Get(ctx context.Context, arg Item) (*CachedStream, error) {if !fc.available(ctx) {reader, err := fc.getReader(ctx, arg)if err != nil {@@ -87,7 +91,7 @@ func (fc *fileCache) Get(ctx context.Context, arg fmt.Stringer) (*CachedStream,return &CachedStream{Reader: reader}, nil}- key := arg.String()+ key := arg.Key()r, w, err := fc.cache.Get(key)if err != nil {return nil, errdiff --git a/core/cache/file_caches_test.go b/core/cache/file_caches_test.goindex 2724d301d..933e27538 100644--- a/core/cache/file_caches_test.go+++ b/core/cache/file_caches_test.go@@ -2,7 +2,6 @@ package cacheimport ("context"- "fmt""io""io/ioutil""os"@@ -53,9 +52,9 @@ var _ = Describe("File Caches", func() {Describe("FileCache", func() {It("caches data if cache is enabled", func() {called := false- fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {+ fc := callNewFileCache("test", "1KB", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {called = true- return strings.NewReader(arg.String()), nil+ return strings.NewReader(arg.Key()), nil})// First call is a MISSs, err := fc.Get(context.TODO(), &testArg{"test"})@@ -74,9 +73,9 @@ var _ = Describe("File Caches", func() {It("does not cache data if cache is disabled", func() {called := false- fc := callNewFileCache("test", "0", "test", 0, func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {+ fc := callNewFileCache("test", "0", "test", 0, func(ctx context.Context, arg Item) (io.Reader, error) {called = true- return strings.NewReader(arg.String()), nil+ return strings.NewReader(arg.Key()), nil})// First call is a MISSs, err := fc.Get(context.TODO(), &testArg{"test"})@@ -97,4 +96,4 @@ var _ = Describe("File Caches", func() {type testArg struct{ s string }-func (t *testArg) String() string { return t.s }+func (t *testArg) Key() string { return t.s }diff --git a/core/media_streamer.go b/core/media_streamer.goindex a673fdf84..e3612400c 100644--- a/core/media_streamer.go+++ b/core/media_streamer.go@@ -40,7 +40,7 @@ type streamJob struct {bitRate int}-func (j *streamJob) String() string {+func (j *streamJob) Key() string {return fmt.Sprintf("%s.%s.%d.%s", j.mf.ID, j.mf.UpdatedAt.Format(time.RFC3339Nano), j.bitRate, j.format)}@@ -170,7 +170,7 @@ func selectTranscodingOptions(ctx context.Context, ds model.DataStore, mf *modelfunc NewTranscodingCache() TranscodingCache {return cache.NewFileCache("Transcoding", conf.Server.TranscodingCacheSize,consts.TranscodingCacheDir, consts.DefaultTranscodingCacheMaxItems,- func(ctx context.Context, arg fmt.Stringer) (io.Reader, error) {+ func(ctx context.Context, arg cache.Item) (io.Reader, error) {job := arg.(*streamJob)t, err := job.ms.ds.Transcoding(ctx).FindByFormat(job.format)if err != nil {diff --git a/go.sum b/go.sumindex 8f38d8fa2..1a6ca160e 100644--- a/go.sum+++ b/go.sum@@ -468,6 +468,7 @@ github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Qgithub.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=+github.com/siddontang/go v0.0.0-20170517070808-cb568a3e5cc0 h1:QIF48X1cihydXibm+4wfAc0r/qyPyuFiPFRNphdMpEE=github.com/siddontang/go v0.0.0-20170517070808-cb568a3e5cc0/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=github.com/siddontang/goredis v0.0.0-20150324035039-760763f78400/go.mod h1:DDcKzU3qCuvj/tPnimWSsZZzvk9qvkvrIL5naVBPh5s=github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d h1:NVwnfyR3rENtlz62bcrkXME3INVUa4lcdGt+opvxExs=