rename cache to storage

This commit is contained in:
David Christofas
2020-03-05 15:04:40 +01:00
parent 34dcaf9ffe
commit 0978653f90
6 changed files with 42 additions and 42 deletions

View File

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

View File

@@ -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
}

View File

@@ -1,3 +1,3 @@
package cache
package storage
// TODO: implement filesystem cache for longer persistence

View File

@@ -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
}

View File

@@ -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)
}

View File

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