Progress meow
This commit is contained in:
parent
490b788e7b
commit
814316ab1e
11 changed files with 279 additions and 35 deletions
|
@ -3,6 +3,7 @@ package storage
|
|||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-webauthn/webauthn/webauthn"
|
||||
|
@ -94,6 +95,23 @@ type RemoteAccountLinks struct {
|
|||
func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
|
||||
log.Trace().Caller().Send()
|
||||
log.Debug().Str("account-handle", handle).Msg("Looking for account by handle")
|
||||
log.Debug().Str("account-handle", handle).Msg("Checking if there's a cache hit")
|
||||
|
||||
// Try and find the account in cache first
|
||||
cacheAccId, err := s.cacheHandleToAccUid(handle)
|
||||
if err == nil {
|
||||
log.Info().Str("account-handle", handle).Msg("Hit account handle in cache")
|
||||
// Then always load via id since unique key access should be faster than string matching
|
||||
return s.FindAccountById(*cacheAccId)
|
||||
} else {
|
||||
if !errors.Is(err, errCacheNotFound) {
|
||||
log.Error().Err(err).Str("account-handle", handle).Msg("Problem while checking cache for account")
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Failed to find in cache, go the slow route of hitting the db
|
||||
log.Debug().Str("account-handle", handle).Msg("Didn't hit account in cache, going to db")
|
||||
name, server, err := ap.SplitFullHandle(handle)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("account-handle", handle).Msg("Failed to split up account handle")
|
||||
|
@ -110,7 +128,19 @@ func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
|
|||
}
|
||||
return nil, res.Error
|
||||
}
|
||||
log.Info().Str("account-handle", handle).Msg("Found account")
|
||||
log.Info().Str("account-handle", handle).Msg("Found account, also inserting into cache")
|
||||
if err = s.cache.Set(cacheUserIdToAccPrefix+acc.ID, &acc); err != nil {
|
||||
log.Warn().
|
||||
Err(err).
|
||||
Str("account-handle", handle).
|
||||
Msg("Found account but failed to insert into cache")
|
||||
}
|
||||
if err = s.cache.Set(cacheUserHandleToIdPrefix+strings.TrimLeft(handle, "@"), acc.ID); err != nil {
|
||||
log.Warn().
|
||||
Err(err).
|
||||
Str("account-handle", handle).
|
||||
Msg("Failed to store handle to id in cache")
|
||||
}
|
||||
return &acc, nil
|
||||
}
|
||||
|
||||
|
@ -118,8 +148,18 @@ func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
|
|||
func (s *Storage) FindAccountById(id string) (*Account, error) {
|
||||
log.Trace().Caller().Send()
|
||||
log.Debug().Str("account-id", id).Msg("Looking for account by id")
|
||||
acc := Account{}
|
||||
res := s.db.First(&acc, id)
|
||||
log.Debug().Str("account-id", id).Msg("First trying to hit cache")
|
||||
acc, err := s.cacheAccIdToData(id)
|
||||
if err == nil {
|
||||
log.Info().Str("account-id", id).Msg("Found account in cache")
|
||||
return acc, nil
|
||||
} else if !errors.Is(err, errCacheNotFound) {
|
||||
log.Error().Err(err).Str("account-id", id).Msg("Error while looking for account in cache")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debug().Str("account-id", id).Msg("Didn't hit account in cache, checking db")
|
||||
res := s.db.First(acc, id)
|
||||
if res.Error != nil {
|
||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||
log.Warn().Str("account-id", id).Msg("Account not found")
|
||||
|
@ -128,8 +168,27 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
|
|||
}
|
||||
return nil, res.Error
|
||||
}
|
||||
log.Info().Str("account-id", id).Msg("Found account")
|
||||
return &acc, nil
|
||||
log.Info().Str("account-id", id).Msg("Found account in db, also adding to cache")
|
||||
if err = s.cache.Set(cacheUserIdToAccPrefix+id, acc); err != nil {
|
||||
log.Warn().Err(err).Str("account-id", id).Msg("Failed to add account to cache")
|
||||
}
|
||||
return acc, nil
|
||||
}
|
||||
|
||||
// Update a given account in storage and cache
|
||||
func (s *Storage) UpdateAccount(acc *Account) error {
|
||||
// If the account is nil or doesn't have an id, error out
|
||||
if acc == nil || acc.ID == "" {
|
||||
return ErrInvalidData
|
||||
}
|
||||
res := s.db.Save(acc)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if err := s.cache.Set(cacheUserIdToAccPrefix+acc.ID, acc); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) NewEmptyAccount() (*Account, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue