From 2d39a6df8d94d48a9f1dc995a9e9b285c03fd749 Mon Sep 17 00:00:00 2001 From: Deluan Date: Thu, 9 Apr 2020 13:15:01 -0400 Subject: [PATCH] Remove duplicated fscache creation --- consts/consts.go | 22 ++++++++++++---------- engine/cover.go | 18 ++---------------- engine/engine_suite_test.go | 2 +- engine/file_caches.go | 28 ++++++++++++++++++++++++++++ engine/file_caches_test.go | 30 ++++++++++++++++++++++++++++++ engine/media_streamer.go | 17 +---------------- 6 files changed, 74 insertions(+), 43 deletions(-) create mode 100644 engine/file_caches.go create mode 100644 engine/file_caches_test.go diff --git a/consts/consts.go b/consts/consts.go index ea353c3aa..51d691a21 100644 --- a/consts/consts.go +++ b/consts/consts.go @@ -21,16 +21,6 @@ const ( UIAssetsLocalPath = "ui/build" - TranscodingCacheDir = "cache/transcoding" - DefaultTranscodingCacheSize = 100 * 1024 * 1024 // 100MB - DefaultTranscodingCacheMaxItems = 0 // Unlimited - DefaultTranscodingCacheCleanUpInterval = 10 * time.Minute - - ImageCacheDir = "cache/images" - DefaultImageCacheSize = 100 * 1024 * 1024 // 100MB - DefaultImageCacheMaxItems = 0 // Unlimited - DefaultImageCacheCleanUpInterval = 10 * time.Minute - DevInitialUserName = "admin" DevInitialName = "Dev Admin" @@ -38,6 +28,18 @@ const ( URLPathSubsonicAPI = "/rest" ) +// Cache options +const ( + TranscodingCacheDir = "cache/transcoding" + DefaultTranscodingCacheMaxItems = 0 // Unlimited + + ImageCacheDir = "cache/images" + DefaultImageCacheMaxItems = 0 // Unlimited + + DefaultCacheSize = 100 * 1024 * 1024 // 100MB + DefaultCacheCleanUpInterval = 10 * time.Minute +) + var ( DefaultTranscodings = []map[string]interface{}{ { diff --git a/engine/cover.go b/engine/cover.go index 172329dbd..deccc9414 100644 --- a/engine/cover.go +++ b/engine/cover.go @@ -11,7 +11,6 @@ import ( _ "image/png" "io" "os" - "path/filepath" "strings" "time" @@ -23,7 +22,6 @@ import ( "github.com/dhowden/tag" "github.com/disintegration/imaging" "github.com/djherbis/fscache" - "github.com/dustin/go-humanize" ) type Cover interface { @@ -52,6 +50,7 @@ func (c *cover) Get(ctx context.Context, id string, size int, out io.Writer) err r, w, err := c.cache.Get(cacheKey) if err != nil { log.Error(ctx, "Error reading from image cache", "path", path, "size", size, err) + return err } defer r.Close() if w != nil { @@ -159,18 +158,5 @@ func readFromTag(path string) ([]byte, error) { } func NewImageCache() (ImageCache, error) { - cacheSize, err := humanize.ParseBytes(conf.Server.ImageCacheSize) - if err != nil { - cacheSize = consts.DefaultImageCacheSize - } - lru := fscache.NewLRUHaunter(consts.DefaultImageCacheMaxItems, int64(cacheSize), consts.DefaultImageCacheCleanUpInterval) - h := fscache.NewLRUHaunterStrategy(lru) - cacheFolder := filepath.Join(conf.Server.DataFolder, consts.ImageCacheDir) - log.Info("Creating image cache", "path", cacheFolder, "maxSize", humanize.Bytes(cacheSize), - "cleanUpInterval", consts.DefaultImageCacheCleanUpInterval) - fs, err := fscache.NewFs(cacheFolder, 0755) - if err != nil { - return nil, err - } - return fscache.NewCacheWithHaunter(fs, h) + return newFileCache("image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems) } diff --git a/engine/engine_suite_test.go b/engine/engine_suite_test.go index a781a10a3..9d7f90f1b 100644 --- a/engine/engine_suite_test.go +++ b/engine/engine_suite_test.go @@ -24,7 +24,7 @@ var testCacheDir string var _ = Describe("Engine Suite Setup", func() { BeforeSuite(func() { - testCacheDir, _ = ioutil.TempDir("", "test_cache") + testCacheDir, _ = ioutil.TempDir("", "engine_test_cache") fs, _ := fscache.NewFs(testCacheDir, 0755) testCache, _ = fscache.NewCache(fs, nil) }) diff --git a/engine/file_caches.go b/engine/file_caches.go new file mode 100644 index 000000000..7ab573efa --- /dev/null +++ b/engine/file_caches.go @@ -0,0 +1,28 @@ +package engine + +import ( + "fmt" + "path/filepath" + + "github.com/deluan/navidrome/conf" + "github.com/deluan/navidrome/consts" + "github.com/deluan/navidrome/log" + "github.com/djherbis/fscache" + "github.com/dustin/go-humanize" +) + +func newFileCache(name, cacheSize, cacheFolder string, maxItems int) (fscache.Cache, error) { + size, err := humanize.ParseBytes(cacheSize) + if err != nil { + size = consts.DefaultCacheSize + } + lru := fscache.NewLRUHaunter(maxItems, int64(size), consts.DefaultCacheCleanUpInterval) + h := fscache.NewLRUHaunterStrategy(lru) + cacheFolder = filepath.Join(conf.Server.DataFolder, cacheFolder) + log.Info(fmt.Sprintf("Creating %s cache", name), "path", cacheFolder, "maxSize", humanize.Bytes(size)) + fs, err := fscache.NewFs(cacheFolder, 0755) + if err != nil { + return nil, err + } + return fscache.NewCacheWithHaunter(fs, h) +} diff --git a/engine/file_caches_test.go b/engine/file_caches_test.go new file mode 100644 index 000000000..a41a7316f --- /dev/null +++ b/engine/file_caches_test.go @@ -0,0 +1,30 @@ +package engine + +import ( + "io/ioutil" + "os" + "path/filepath" + + "github.com/deluan/navidrome/conf" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("File Caches", func() { + BeforeEach(func() { + conf.Server.DataFolder, _ = ioutil.TempDir("", "file_caches") + }) + AfterEach(func() { + os.RemoveAll(conf.Server.DataFolder) + }) + + Describe("newFileCache", func() { + It("creates the cache folder", func() { + _, err := newFileCache("test", "1kb", "test", 10) + Expect(err).To(BeNil()) + + _, err = os.Stat(filepath.Join(conf.Server.DataFolder, "test")) + Expect(os.IsNotExist(err)).To(BeFalse()) + }) + }) +}) diff --git a/engine/media_streamer.go b/engine/media_streamer.go index a73e7c02a..af772dade 100644 --- a/engine/media_streamer.go +++ b/engine/media_streamer.go @@ -6,7 +6,6 @@ import ( "io" "mime" "os" - "path/filepath" "time" "github.com/deluan/navidrome/conf" @@ -15,7 +14,6 @@ import ( "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" "github.com/djherbis/fscache" - "github.com/dustin/go-humanize" ) type MediaStreamer interface { @@ -215,18 +213,5 @@ func getFinalCachedSize(r fscache.ReadAtCloser) int64 { } func NewTranscodingCache() (TranscodingCache, error) { - cacheSize, err := humanize.ParseBytes(conf.Server.TranscodingCacheSize) - if err != nil { - cacheSize = consts.DefaultTranscodingCacheSize - } - lru := fscache.NewLRUHaunter(consts.DefaultTranscodingCacheMaxItems, int64(cacheSize), consts.DefaultTranscodingCacheCleanUpInterval) - h := fscache.NewLRUHaunterStrategy(lru) - cacheFolder := filepath.Join(conf.Server.DataFolder, consts.TranscodingCacheDir) - log.Info("Creating transcoding cache", "path", cacheFolder, "maxSize", humanize.Bytes(cacheSize), - "cleanUpInterval", consts.DefaultTranscodingCacheCleanUpInterval) - fs, err := fscache.NewFs(cacheFolder, 0755) - if err != nil { - return nil, err - } - return fscache.NewCacheWithHaunter(fs, h) + return newFileCache("transcoding", conf.Server.TranscodingCacheSize, consts.TranscodingCacheDir, consts.DefaultTranscodingCacheMaxItems) }