thumbnailer: allow passing 0 as cache size

This commit is contained in:
mappu 2017-11-18 12:52:53 +13:00
parent f89726198b
commit 2791bc2cd7
1 changed files with 25 additions and 14 deletions

View File

@ -44,34 +44,42 @@ type Thumbnailer struct {
afmt AspectFormat
sfmt ScaleFormat
thumbCache *lru.Cache // threadsafe
thumbCache *lru.Cache // threadsafe, might be nil
}
func NewThumbnailer(Width, Height, MaxCacheSize int) *Thumbnailer {
func NewThumbnailer(Width, Height int, MaxCacheSize uint) *Thumbnailer {
return NewThumbnailerEx(Width, Height, MaxCacheSize, OUTPUT__DEFAULT, ASPECT__DEFAULT, SCALEFMT__DEFAULT)
}
func NewThumbnailerEx(Width, Height, MaxCacheSize int, of OutputFormat, af AspectFormat, sf ScaleFormat) *Thumbnailer {
thumbCache, err := lru.New(MaxCacheSize)
if err != nil {
panic(err)
}
func NewThumbnailerEx(Width, Height int, MaxCacheSize uint, of OutputFormat, af AspectFormat, sf ScaleFormat) *Thumbnailer {
return &Thumbnailer{
ret := &Thumbnailer{
width: Width,
height: Height,
ofmt: of,
afmt: af,
sfmt: sf,
thumbCache: thumbCache,
thumbCache: nil,
}
if MaxCacheSize > 0 {
thumbCache, err := lru.New(int(MaxCacheSize))
if err != nil {
panic(err)
}
ret.thumbCache = thumbCache
}
return ret
}
func (this *Thumbnailer) RenderFile(absPath string) ([]byte, error) {
thumb, ok := this.thumbCache.Get(absPath)
if ok {
return thumb.([]byte), nil
if this.thumbCache != nil {
thumb, ok := this.thumbCache.Get(absPath)
if ok {
return thumb.([]byte), nil
}
}
// Add to cache
@ -80,8 +88,11 @@ func (this *Thumbnailer) RenderFile(absPath string) ([]byte, error) {
return nil, err
}
this.thumbCache.Add(absPath, thumb)
return thumb.([]byte), nil
if this.thumbCache != nil {
this.thumbCache.Add(absPath, thumb)
}
return thumb, nil
}
func FiletypeSupported(ext string) bool {