photoprism/internal/entity/album_cache.go
Michael Mayer d8e0364dbb Search: Ignore public album filter if "Private" feat is disabled #2570
This needs to be very well tested and discussed, as these changes can
lead to private photos being accidentally published. Thank you!

Signed-off-by: Michael Mayer <michael@photoprism.app>
2022-08-01 15:57:19 +02:00

43 lines
914 B
Go

package entity
import (
"fmt"
"time"
gc "github.com/patrickmn/go-cache"
"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?
if uid == "" || !rnd.EntityUID(uid, 'a') {
return m, fmt.Errorf("invalid album uid")
}
// 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
}
}