Files
filebrowser/backend/internal/state/user_cache.go
T
+11 45a14bf2f2 Beta/v2.1.0 (#3046)
Signed-off-by: MichaIng <micha@dietpi.com>
Co-authored-by: Kevin B. <kurami32_kbm@proton.me>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Mattijs Bliek <mattijs@mattijsbliek.com>
Co-authored-by: GammelSami <39372285+GammelSami@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: yzxcj797 <54314860+yzxcj797@users.noreply.github.com>
Co-authored-by: yzxcj797 <yzxcj797@users.noreply.github.com>
Co-authored-by: DireMunchkin <dire.munchkin@proton.me>
Co-authored-by: Chris <39135018+Chri6s@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Aditya Raj Singh <github@adityaraj.com>
Co-authored-by: tbsagdic <86909795+tbsagdic@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Ethan Lee Qi Yang <pluscompanyhqDEPT@gmail.com>
Co-authored-by: Aditya Raj Singh <aditya@bncw.in>
Co-authored-by: MichaIng <micha@dietpi.com>
Co-authored-by: FandacqkaCorp <david@davidyloreto.com>
Co-authored-by: FandacqkaCorp <275647464+FandacqkaCorp@users.noreply.github.com>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: connorjfarrell <connorjfarrell@users.noreply.github.com>
2026-09-30 13:04:20 -05:00

115 lines
2.7 KiB
Go

package state
import (
"fmt"
"time"
"github.com/gtsteffaniak/filebrowser/backend/internal/database/users"
"github.com/gtsteffaniak/filebrowser/backend/internal/errors"
"github.com/gtsteffaniak/go-cache/cache"
)
const userCacheTTL = 30 * time.Minute
var userRecordCache = cache.NewCache[*users.User](userCacheTTL, userCacheTTL)
func userCacheKeyID(id uint64) string {
return fmt.Sprintf("id:%d", id)
}
func userCacheKeyName(username string) string {
return fmt.Sprintf("name:%s", username)
}
func putUserInCache(user *users.User) {
if user == nil {
return
}
cached := cloneUserPtr(user)
userRecordCache.Set(userCacheKeyName(user.Username), cached)
if user.ID != 0 {
userRecordCache.Set(userCacheKeyID(user.ID), cached)
}
}
func deleteUserFromCache(user *users.User) {
if user == nil {
return
}
userRecordCache.Delete(userCacheKeyName(user.Username))
if user.ID != 0 {
userRecordCache.Delete(userCacheKeyID(user.ID))
}
}
func clearUserRecordCache() {
userRecordCache.ClearAll()
}
func cloneUserPtr(user *users.User) *users.User {
if user == nil {
return nil
}
copy := *user
copyUserSlices(&copy, user)
return &copy
}
func loadUserByUsernameFromDB(username string) (*users.User, error) {
user, err := sqlDb.GetUserByUsername(username)
if err != nil && err.Error() == "user not found" {
return nil, errors.ErrNotExist
}
return user, err
}
func loadUserByIDFromDB(id uint64) (*users.User, error) {
user, err := sqlDb.GetUserByID(id)
if err != nil && err.Error() == "user not found" {
return nil, errors.ErrNotExist
}
return user, err
}
func getCachedUserByUsername(username string) (*users.User, bool) {
if cached, ok := userRecordCache.Get(userCacheKeyName(username)); ok && cached != nil {
return cloneUserPtr(cached), true
}
return nil, false
}
func getCachedUserByID(id uint64) (*users.User, bool) {
if cached, ok := userRecordCache.Get(userCacheKeyID(id)); ok && cached != nil {
return cloneUserPtr(cached), true
}
return nil, false
}
// loadExistingUserLocked loads a mutable user copy for updates (callers must hold usersMux).
func loadExistingUserLocked(user *users.User) (*users.User, error) {
if user == nil {
return nil, fmt.Errorf("user not found in cache")
}
if user.ID != 0 {
if cached, ok := getCachedUserByID(user.ID); ok {
return cached, nil
}
fromDB, err := loadUserByIDFromDB(user.ID)
if err != nil {
return nil, err
}
return cloneUserPtr(fromDB), nil
}
if user.Username != "" {
if cached, ok := getCachedUserByUsername(user.Username); ok {
return cached, nil
}
fromDB, err := loadUserByUsernameFromDB(user.Username)
if err != nil {
return nil, err
}
return cloneUserPtr(fromDB), nil
}
return nil, fmt.Errorf("user not found in cache")
}