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

View File

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