2021-01-08 12:20:41 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
2021-08-30 18:58:27 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/query"
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/get"
|
2021-01-08 12:20:41 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/thumb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxAge represents a cache TTL in seconds.
|
|
|
|
type MaxAge int
|
|
|
|
|
|
|
|
// String returns the cache TTL in seconds as string.
|
|
|
|
func (a MaxAge) String() string {
|
|
|
|
return strconv.Itoa(int(a))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default cache TTL times in seconds.
|
|
|
|
var (
|
|
|
|
CoverCacheTTL MaxAge = 3600 // 1 hour
|
|
|
|
ThumbCacheTTL MaxAge = 3600 * 24 * 90 // ~ 3 months
|
|
|
|
)
|
|
|
|
|
|
|
|
type ThumbCache struct {
|
|
|
|
FileName string
|
|
|
|
ShareName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByteCache struct {
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// CacheKey returns a cache key string based on namespace, uid and name.
|
|
|
|
func CacheKey(ns, uid, name string) string {
|
|
|
|
return fmt.Sprintf("%s:%s:%s", ns, uid, name)
|
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
// RemoveFromFolderCache removes an item from the folder cache e.g. after indexing.
|
|
|
|
func RemoveFromFolderCache(rootName string) {
|
2022-10-15 21:54:11 +02:00
|
|
|
cache := get.FolderCache()
|
2021-01-08 13:29:01 +01:00
|
|
|
|
|
|
|
cacheKey := fmt.Sprintf("folder:%s:%t:%t", rootName, true, false)
|
|
|
|
|
|
|
|
cache.Delete(cacheKey)
|
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateAlbumFolderCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 13:29:01 +01:00
|
|
|
log.Debugf("removed %s from cache", cacheKey)
|
|
|
|
}
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
// RemoveFromAlbumCoverCache removes covers by album UID e.g. after adding or removing photos.
|
|
|
|
func RemoveFromAlbumCoverCache(uid string) {
|
2022-10-15 21:54:11 +02:00
|
|
|
cache := get.CoverCache()
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-09-05 13:48:53 +02:00
|
|
|
for thumbName := range thumb.Sizes {
|
|
|
|
cacheKey := CacheKey(albumCover, uid, string(thumbName))
|
2021-01-08 12:20:41 +01:00
|
|
|
|
|
|
|
cache.Delete(cacheKey)
|
|
|
|
|
|
|
|
log.Debugf("removed %s from cache", cacheKey)
|
|
|
|
}
|
2021-08-30 18:58:27 +02:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateAlbumCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
2021-01-08 12:20:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlushCoverCache clears the complete cover cache.
|
|
|
|
func FlushCoverCache() {
|
2022-10-15 21:54:11 +02:00
|
|
|
get.CoverCache().Flush()
|
2021-01-08 12:20:41 +01:00
|
|
|
|
2021-10-01 16:44:50 +02:00
|
|
|
if err := query.UpdateCovers(); err != nil {
|
2021-09-23 23:46:17 +02:00
|
|
|
log.Error(err)
|
2021-08-30 18:58:27 +02:00
|
|
|
}
|
|
|
|
|
2021-01-08 12:20:41 +01:00
|
|
|
log.Debugf("albums: flushed cover cache")
|
|
|
|
}
|