From ae7c1f6538b097cd0250da949ee7f6731f2af0ff Mon Sep 17 00:00:00 2001 From: AgustinSRG Date: Sat, 23 Sep 2023 12:40:35 +0200 Subject: [PATCH] Backend: Pre-cache tags and albums on vault unlock #30 --- backend/session.go | 8 ++++++++ backend/vault_album_manager.go | 18 ++++++++++++++++++ backend/vault_tag_manager.go | 12 ++++++++++++ 3 files changed, 38 insertions(+) diff --git a/backend/session.go b/backend/session.go index b54e44af..733e3cbc 100644 --- a/backend/session.go +++ b/backend/session.go @@ -57,6 +57,8 @@ func (sm *SessionManager) CreateSession(user string, key []byte, root bool, writ sm.lock.Lock() + isFirstSession := len(sm.sessions) == 0 + newSession := ActiveSession{ id: sessionId, user: user, @@ -77,6 +79,12 @@ func (sm *SessionManager) CreateSession(user string, key []byte, root bool, writ LogError(err) } + if isFirstSession { + // Pre-cache tags and albums + go sm.vault.tags.PreCacheTags(key) + go sm.vault.albums.PreCacheAlbums(key) + } + return sessionId } diff --git a/backend/vault_album_manager.go b/backend/vault_album_manager.go index 423c652e..9d2398db 100644 --- a/backend/vault_album_manager.go +++ b/backend/vault_album_manager.go @@ -435,3 +435,21 @@ func (am *VaultAlbumsManager) OnMediaThumbnailUpdate(media_id uint64, key []byte return nil } + +// Reads the albums and pre-caches them on vault unlock +// key - Vault decryption key +func (am *VaultAlbumsManager) PreCacheAlbums(key []byte) { + data, err := am.ReadAlbums(key) + + if err != nil { + LogError(err) + return + } + + // Pre-cache thumbnails + for album_id := range data.Albums { + am.thumbnail_cache.GetAlbumThumbnail(album_id, key) + } + + LogDebug("Pre-cached albums") +} diff --git a/backend/vault_tag_manager.go b/backend/vault_tag_manager.go index 48202bfd..9af698ec 100644 --- a/backend/vault_tag_manager.go +++ b/backend/vault_tag_manager.go @@ -610,3 +610,15 @@ func (tm *VaultTagManager) RandomTaggedMedia(tag_name string, key []byte, seed i return values, tag_id, nil } + +// Reads the tags and pre-caches them on vault unlock +// key - Vault decryption key +func (tm *VaultTagManager) PreCacheTags(key []byte) { + _, err := tm.ReadList(key) + + if err != nil { + LogError(err) + } + + LogDebug("Pre-cached tags") +}