add GetExtendTTL
This commit is contained in:
@@ -95,6 +95,20 @@ func (i *ImageCache) GetResetTTL(key string, resetTTL bool) (image.Image, error)
|
|||||||
return nil, ErrNotFound
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Gets the image if it exists and extends TTL to time.Now + ttl iff the image would expire before then
|
||||||
|
func (i *ImageCache) GetExtendTTL(key string, ttl time.Duration) (image.Image, error) {
|
||||||
|
i.mu.RLock()
|
||||||
|
defer i.mu.RUnlock()
|
||||||
|
if v, ok := i.cache[key]; ok {
|
||||||
|
v.lastAccessed = time.Now().Unix()
|
||||||
|
if v.expiresAt < time.Now().Add(ttl).Unix() {
|
||||||
|
v.expiresAt = time.Now().Add(ttl).Unix()
|
||||||
|
}
|
||||||
|
return v.val, nil
|
||||||
|
}
|
||||||
|
return nil, ErrNotFound
|
||||||
|
}
|
||||||
|
|
||||||
func (i *ImageCache) GetWithNewTTL(key string, newTtl time.Duration) (image.Image, error) {
|
func (i *ImageCache) GetWithNewTTL(key string, newTtl time.Duration) (image.Image, error) {
|
||||||
i.mu.RLock()
|
i.mu.RLock()
|
||||||
defer i.mu.RUnlock()
|
defer i.mu.RUnlock()
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ func NewImageManager(ctx context.Context, s *ServerManager, baseCacheDir string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *ImageManager) GetAlbumThumbnailFromCache(albumID string) (image.Image, bool) {
|
func (i *ImageManager) GetAlbumThumbnailFromCache(albumID string) (image.Image, bool) {
|
||||||
if img, err := i.thumbnailCache.GetResetTTL(albumID, true); err == nil && img != nil {
|
img, err := i.thumbnailCache.GetExtendTTL(albumID, i.thumbnailCache.DefaultTTL)
|
||||||
|
if err == nil && img != nil {
|
||||||
return img, true
|
return img, true
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
|
|||||||
Reference in New Issue
Block a user