load or store for account

This commit is contained in:
crn4
2025-10-29 23:35:16 +01:00
parent 60e3bf4084
commit 479c75f827
3 changed files with 20 additions and 6 deletions

View File

@@ -1,6 +1,9 @@
package types
import "sync"
import (
"context"
"sync"
)
type Holder struct {
mu sync.RWMutex
@@ -24,3 +27,17 @@ func (h *Holder) AddAccount(account *Account) {
defer h.mu.Unlock()
h.accounts[account.Id] = account
}
func (h *Holder) LoadOrStoreFunc(id string, accGetter func(context.Context, string) (*Account, error)) (*Account, error) {
h.mu.Lock()
defer h.mu.Unlock()
if acc, ok := h.accounts[id]; ok {
return acc, nil
}
account, err := accGetter(context.Background(), id)
if err != nil {
return nil, err
}
h.accounts[id] = account
return account, nil
}