diff --git a/internal/provider/balance_history.go b/internal/provider/balance_history.go index e6782c07c..dc8d31aeb 100644 --- a/internal/provider/balance_history.go +++ b/internal/provider/balance_history.go @@ -28,6 +28,7 @@ import ( "sync" "time" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/lastgood" "github.com/yetone/magpie/internal/lazyre" ) @@ -75,16 +76,34 @@ var balanceHistMu sync.Mutex func balanceHistPath() string { return filepath.Join(filepath.Dir(Path()), "balance-history.json") } -func readBalanceHist() balanceHist { - h := balanceHist{} - if b, err := os.ReadFile(balanceHistPath()); err == nil { +// balanceHistShared is the file as last parsed, while it is unchanged +// (filememo): every usage read gives each balance its trend from it, and +// the file runs to hundreds of KB. It is shared: never changed. +func balanceHistShared() balanceHist { + h, err := filememo.Read("balance-history", balanceHistPath(), func(b []byte) (balanceHist, error) { + h := balanceHist{} if json.Unmarshal(b, &h) != nil || h == nil { - return balanceHist{} + return balanceHist{}, nil } + return h, nil + }) + if err != nil || h == nil { + return balanceHist{} } return h } +// readBalanceHist is the file for a caller to change and write: a copy of +// balanceHistShared, each key's points its own. +func readBalanceHist() balanceHist { + h := balanceHistShared() + out := make(balanceHist, len(h)) + for k, pts := range h { + out[k] = slices.Clone(pts) + } + return out +} + func writeBalanceHist(h balanceHist) { b, err := json.Marshal(h) if err != nil { @@ -242,7 +261,7 @@ func noteBalanceHistory(qs []SubscriptionQuota, now time.Time) { continue } if h == nil { - h = readBalanceHist() + h = balanceHistShared() // read only from here on } key := quotaHistKey(q.Provider, q.User) cs := balanceCurrencies(q.Balance) diff --git a/internal/provider/balance_shared.go b/internal/provider/balance_shared.go index 4183a61f3..c2e4eee2e 100644 --- a/internal/provider/balance_shared.go +++ b/internal/provider/balance_shared.go @@ -72,7 +72,7 @@ func sharedBalanceHist(cards []SubscriptionQuota) balanceHist { if sharesAccountBalance(q) { balanceHistMu.Lock() defer balanceHistMu.Unlock() - return readBalanceHist() + return balanceHistShared() // shareBalances only reads it } } return nil