2022-08-01 15:57:19 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
gc "github.com/patrickmn/go-cache"
|
|
|
|
|
2022-09-28 09:01:17 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-08-01 15:57:19 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/rnd"
|
|
|
|
)
|
|
|
|
|
|
|
|
var albumCache = gc.New(15*time.Minute, 15*time.Minute)
|
|
|
|
|
|
|
|
// FlushAlbumCache resets the album cache.
|
|
|
|
func FlushAlbumCache() {
|
|
|
|
albumCache.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CachedAlbumByUID returns an existing album or nil if not found.
|
|
|
|
func CachedAlbumByUID(uid string) (m Album, err error) {
|
|
|
|
// Valid album UID?
|
2022-09-30 00:42:19 +02:00
|
|
|
if uid == "" || rnd.InvalidUID(uid, AlbumUID) {
|
2022-09-28 09:01:17 +02:00
|
|
|
return m, fmt.Errorf("invalid album uid %s", clean.LogQuote(uid))
|
2022-08-01 15:57:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cached?
|
|
|
|
if cacheData, ok := albumCache.Get(uid); ok {
|
|
|
|
log.Tracef("album: cache hit for %s", uid)
|
|
|
|
return cacheData.(Album), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find in database.
|
|
|
|
m = Album{}
|
|
|
|
|
|
|
|
if r := Db().First(&m, "album_uid = ?", uid); r.RecordNotFound() {
|
|
|
|
return m, fmt.Errorf("album not found")
|
|
|
|
} else if r.Error != nil {
|
|
|
|
return m, r.Error
|
|
|
|
} else {
|
|
|
|
albumCache.SetDefault(m.AlbumUID, m)
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
}
|