Add dedicated Item interface for cache items

This commit is contained in:
Deluan 2020-10-24 15:10:27 -04:00
parent 7eaa42797a
commit 197d4024f7
5 changed files with 21 additions and 17 deletions

View File

@ -43,14 +43,14 @@ type artwork struct {
}
type imageInfo struct {
c *artwork
a *artwork
id string
path string
size int
lastUpdate 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, err

View File

@ -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, err

View File

@ -2,7 +2,6 @@ package cache
import (
"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 MISS
s, 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 MISS
s, 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 }

View File

@ -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 *model
func 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 {

1
go.sum
View File

@ -468,6 +468,7 @@ github.com/shirou/w32 v0.0.0-20160930032740-bb4de0191aa4/go.mod h1:qsXQc7+bwAM3Q
github.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=