From 0978653f9077d7ddebbc67beb9fec206b19e9201 Mon Sep 17 00:00:00 2001 From: David Christofas Date: Thu, 5 Mar 2020 15:04:40 +0100 Subject: [PATCH] rename cache to storage --- pkg/service/v0/service.go | 6 ++--- pkg/thumbnails/cache/inmemory.go | 22 ---------------- .../{cache => storage}/filesystem.go | 2 +- pkg/thumbnails/storage/inmemory.go | 22 ++++++++++++++++ .../{cache/cache.go => storage/storage.go} | 6 ++--- pkg/thumbnails/thumbnails.go | 26 +++++++++---------- 6 files changed, 42 insertions(+), 42 deletions(-) delete mode 100644 pkg/thumbnails/cache/inmemory.go rename pkg/thumbnails/{cache => storage}/filesystem.go (78%) create mode 100644 pkg/thumbnails/storage/inmemory.go rename pkg/thumbnails/{cache/cache.go => storage/storage.go} (54%) diff --git a/pkg/service/v0/service.go b/pkg/service/v0/service.go index d652ecf759..4d3b45e5a5 100644 --- a/pkg/service/v0/service.go +++ b/pkg/service/v0/service.go @@ -7,8 +7,8 @@ import ( "github.com/go-chi/chi" "github.com/owncloud/ocis-thumbnails/pkg/config" "github.com/owncloud/ocis-thumbnails/pkg/thumbnails" - "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/cache" "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/imgsource" + "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage" ) // Service defines the extension handlers. @@ -28,7 +28,7 @@ func NewService(opts ...Option) Service { config: options.Config, mux: m, manager: thumbnails.SimpleManager{ - Cache: cache.NewInMemoryCache(), + Storage: storage.NewInMemoryStorage(), }, source: imgsource.WebDav{ Basepath: "http://localhost:9140/remote.php/webdav/", @@ -77,7 +77,7 @@ func (g Thumbnails) Thumbnails(w http.ResponseWriter, r *http.Request) { Encoder: encoder, } - thumbnail := g.manager.GetCached(ctx) + thumbnail := g.manager.GetStored(ctx) if thumbnail != nil { w.Write(thumbnail) return diff --git a/pkg/thumbnails/cache/inmemory.go b/pkg/thumbnails/cache/inmemory.go deleted file mode 100644 index d9c45fb663..0000000000 --- a/pkg/thumbnails/cache/inmemory.go +++ /dev/null @@ -1,22 +0,0 @@ -package cache - -import "image" - -func NewInMemoryCache() InMemoryCache { - return InMemoryCache{ - store: make(map[string]image.Image), - } -} - -type InMemoryCache struct { - store map[string]image.Image -} - -func (fsc InMemoryCache) Get(key string) image.Image { - return fsc.store[key] -} - -func (fsc InMemoryCache) Set(key string, thumbnail image.Image) (image.Image, error) { - fsc.store[key] = thumbnail - return thumbnail, nil -} diff --git a/pkg/thumbnails/cache/filesystem.go b/pkg/thumbnails/storage/filesystem.go similarity index 78% rename from pkg/thumbnails/cache/filesystem.go rename to pkg/thumbnails/storage/filesystem.go index b3e25a4d8c..db9db0b038 100644 --- a/pkg/thumbnails/cache/filesystem.go +++ b/pkg/thumbnails/storage/filesystem.go @@ -1,3 +1,3 @@ -package cache +package storage // TODO: implement filesystem cache for longer persistence diff --git a/pkg/thumbnails/storage/inmemory.go b/pkg/thumbnails/storage/inmemory.go new file mode 100644 index 0000000000..307a847192 --- /dev/null +++ b/pkg/thumbnails/storage/inmemory.go @@ -0,0 +1,22 @@ +package storage + +import "image" + +func NewInMemoryStorage() InMemoryStorage { + return InMemoryStorage{ + store: make(map[string]image.Image), + } +} + +type InMemoryStorage struct { + store map[string]image.Image +} + +func (fsc InMemoryStorage) Get(key string) image.Image { + return fsc.store[key] +} + +func (fsc InMemoryStorage) Set(key string, thumbnail image.Image) (image.Image, error) { + fsc.store[key] = thumbnail + return thumbnail, nil +} diff --git a/pkg/thumbnails/cache/cache.go b/pkg/thumbnails/storage/storage.go similarity index 54% rename from pkg/thumbnails/cache/cache.go rename to pkg/thumbnails/storage/storage.go index 71db593cdb..11a41771ef 100644 --- a/pkg/thumbnails/cache/cache.go +++ b/pkg/thumbnails/storage/storage.go @@ -1,11 +1,11 @@ -package cache +package storage import ( "image" ) -// Cache defines the interface for a thumbnail cache. -type Cache interface { +// Storage defines the interface for a thumbnail store. +type Storage interface { Get(key string) image.Image Set(key string, thumbnail image.Image) (image.Image, error) } diff --git a/pkg/thumbnails/thumbnails.go b/pkg/thumbnails/thumbnails.go index 5f1614adda..61b93bf6eb 100644 --- a/pkg/thumbnails/thumbnails.go +++ b/pkg/thumbnails/thumbnails.go @@ -7,7 +7,7 @@ import ( "time" "github.com/nfnt/resize" - "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/cache" + "github.com/owncloud/ocis-thumbnails/pkg/thumbnails/storage" ) // ThumbnailContext bundles information needed to generate a thumbnail for afile @@ -22,20 +22,20 @@ type ThumbnailContext struct { type Manager interface { // Get will return a thumbnail for a file Get(ThumbnailContext, image.Image) ([]byte, error) - GetCached(ThumbnailContext) []byte + GetStored(ThumbnailContext) []byte } // SimpleManager is a simple implementation of Manager type SimpleManager struct { - Cache cache.Cache + Storage storage.Storage } // Get implements the Get Method of Manager func (s SimpleManager) Get(ctx ThumbnailContext, img image.Image) ([]byte, error) { thumbnail := s.generate(ctx, img) - key := buildCacheKey(ctx) - s.Cache.Set(key, thumbnail) + key := buildKey(ctx) + s.Storage.Set(key, thumbnail) buf := new(bytes.Buffer) err := ctx.Encoder.Encode(buf, thumbnail) @@ -45,16 +45,16 @@ func (s SimpleManager) Get(ctx ThumbnailContext, img image.Image) ([]byte, error return buf.Bytes(), nil } -// GetCached tries to get the cached thumbnail and return it. -// If there is no cached thumbnail it will return nil -func (s SimpleManager) GetCached(ctx ThumbnailContext) []byte { - key := buildCacheKey(ctx) - cached := s.Cache.Get(key) - if cached == nil { +// GetStored tries to get the stored thumbnail and return it. +// If there is no stored thumbnail it will return nil +func (s SimpleManager) GetStored(ctx ThumbnailContext) []byte { + key := buildKey(ctx) + stored := s.Storage.Get(key) + if stored == nil { return nil } buf := new(bytes.Buffer) - ctx.Encoder.Encode(buf, cached) + ctx.Encoder.Encode(buf, stored) return buf.Bytes() } @@ -66,7 +66,7 @@ func (s SimpleManager) generate(ctx ThumbnailContext, img image.Image) image.Ima return thumbnail } -func buildCacheKey(ctx ThumbnailContext) string { +func buildKey(ctx ThumbnailContext) string { parts := []string{ ctx.ImagePath, string(ctx.Width) + "x" + string(ctx.Height),