Skip to content

Commit

Permalink
Backend: Add cache
Browse files Browse the repository at this point in the history
closes #28
  • Loading branch information
AgustinSRG committed Sep 3, 2023
1 parent 82cda9d commit cf0685d
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 20 deletions.
69 changes: 61 additions & 8 deletions backend/api_albums.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand All @@ -23,6 +24,52 @@ type AlbumAPIItemMinified struct {
Name string `json:"name"`
}

func getAlbumThumbnail(album_id uint64, album *VaultAlbumData, session *ActiveSession) string {
if album.List == nil || len(album.List) == 0 {
return ""
}

media_id := album.List[0]

ok, has_thumbnail, thumbnail_asset := GetVault().albums.FindCachedThumbnailAsset(album_id)

if ok {
if has_thumbnail {
return "/assets/b/" + fmt.Sprint(media_id) + "/" + fmt.Sprint(thumbnail_asset) + "/thumbnail.jpg" + "?fp=" + GetVault().credentials.GetFingerprint()
} else {
return ""
}
}

media := GetVault().media.AcquireMediaResource(media_id)

meta, err := media.ReadMetadata(session.key)

GetVault().media.ReleaseMediaResource(media_id)

if err != nil {
LogError(err)
}

if meta == nil {
return ""
}

has_thumbnail = meta.ThumbnailReady
thumbnail_asset = meta.ThumbnailAsset

GetVault().albums.thumbnail_cache[album_id] = ThumbnailCacheEntry{
has_thumbnail: has_thumbnail,
asset: thumbnail_asset,
}

if has_thumbnail {
return "/assets/b/" + fmt.Sprint(media_id) + "/" + fmt.Sprint(thumbnail_asset) + "/thumbnail.jpg" + "?fp=" + GetVault().credentials.GetFingerprint()
} else {
return ""
}
}

func api_getAlbums(response http.ResponseWriter, request *http.Request) {
session := GetSessionFromRequest(request)

Expand All @@ -31,39 +78,45 @@ func api_getAlbums(response http.ResponseWriter, request *http.Request) {
return
}

minMode := request.URL.Query().Get("mode") == "min"

if !minMode {
GetVault().albums.thumbnail_cache_lock.Lock()
}

albums, err := GetVault().albums.ReadAlbums(session.key)

if err != nil {
if !minMode {
GetVault().albums.thumbnail_cache_lock.Unlock()
}

LogError(err)

ReturnAPIError(response, 500, "INTERNAL_ERROR", "Internal server error, Check the logs for details.")
return
}

if request.URL.Query().Get("mode") != "min" {
if !minMode {
result := make([]AlbumAPIItem, len(albums.Albums))

i := 0

for album_id, album := range albums.Albums {
thumbnail := ""

if len(album.List) > 0 {
media_info := GetMediaMinInfo(album.List[0], session)
thumbnail = media_info.Thumbnail
}

result[i] = AlbumAPIItem{
Id: album_id,
Name: album.Name,
Size: len(album.List),
Thumbnail: thumbnail,
Thumbnail: getAlbumThumbnail(album_id, album, session),
LastModified: album.LastModified,
}

i++
}

GetVault().albums.thumbnail_cache_lock.Unlock()

jsonResult, err := json.Marshal(result)

if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions backend/api_media_thumbnail.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ func api_editMediaThumbnail(response http.ResponseWriter, request *http.Request)

media.ReleaseAsset(thumb_asset)

GetVault().albums.OnMediaThumbnailUpdate(media_id, session.key)

if err != nil {
LogError(err)

Expand Down
16 changes: 14 additions & 2 deletions backend/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ type UserConfig struct {

// User configuration manager
type UserConfigManager struct {
file string // User config file
lock *ReadWriteLock // Lock to control access to the file
file string // User config file
cache *UserConfig // Cache
lock *ReadWriteLock // Lock to control access to the file
}

// Initializes user config manager
// base_path - Vault path
func (uc *UserConfigManager) Initialize(base_path string) {
uc.cache = nil
uc.file = path.Join(base_path, "user_config.pmv")
uc.lock = CreateReadWriteLock()
}
Expand All @@ -67,6 +69,10 @@ func (uc *UserConfigManager) Initialize(base_path string) {
// key - Vault decryption key
// Returns user config data
func (uc *UserConfigManager) Read(key []byte) (*UserConfig, error) {
if uc.cache != nil {
return uc.cache, nil
}

if _, err := os.Stat(uc.file); err == nil {
// Load file
b, err := os.ReadFile(uc.file)
Expand All @@ -89,6 +95,8 @@ func (uc *UserConfigManager) Read(key []byte) (*UserConfig, error) {
return nil, err
}

uc.cache = &mp

return &mp, nil
} else if errors.Is(err, os.ErrNotExist) {
// Default config
Expand All @@ -100,6 +108,8 @@ func (uc *UserConfigManager) Read(key []byte) (*UserConfig, error) {
ImageResolutions: make([]UserConfigImageResolution, 0),
}

uc.cache = &mp

return &mp, nil
} else {
return nil, err
Expand Down Expand Up @@ -140,5 +150,7 @@ func (uc *UserConfigManager) Write(data *UserConfig, key []byte) error {

err = RenameAndReplace(tmpFile, uc.file)

uc.cache = data

return err
}
Loading

0 comments on commit cf0685d

Please sign in to comment.