Improved IA currency caching
Some checks failed
Run Integration Tests / test (push) Failing after 40s

This commit is contained in:
partisan 2025-06-29 08:11:02 +02:00
parent 67eefc2c7a
commit 7dbdcbe202

View file

@ -16,9 +16,8 @@ import (
// ExchangeRateCache holds currency rates with automatic refresh // ExchangeRateCache holds currency rates with automatic refresh
var ( var (
exchangeRates = make(map[string]float64) exchangeRates = make(map[string]float64)
lastUpdated time.Time nextUpdateTime time.Time
exchangeCacheMutex sync.RWMutex exchangeCacheMutex sync.RWMutex
cacheExpiration = 1 * time.Hour
allCurrencies []string allCurrencies []string
) )
@ -45,8 +44,9 @@ func UpdateExchangeRates() error {
} }
var apiResponse struct { var apiResponse struct {
Result string `json:"result"` Result string `json:"result"`
Rates map[string]float64 `json:"rates"` Rates map[string]float64 `json:"rates"`
TimeNextUpdateUnix int64 `json:"time_next_update_unix"`
} }
if err := json.Unmarshal(body, &apiResponse); err != nil { if err := json.Unmarshal(body, &apiResponse); err != nil {
return err return err
@ -58,7 +58,7 @@ func UpdateExchangeRates() error {
// Cache the rates // Cache the rates
exchangeRates = apiResponse.Rates exchangeRates = apiResponse.Rates
lastUpdated = time.Now() nextUpdateTime = time.Unix(apiResponse.TimeNextUpdateUnix, 0)
// Update list of all currencies // Update list of all currencies
allCurrencies = make([]string, 0, len(exchangeRates)) allCurrencies = make([]string, 0, len(exchangeRates))
@ -66,7 +66,7 @@ func UpdateExchangeRates() error {
allCurrencies = append(allCurrencies, currency) allCurrencies = append(allCurrencies, currency)
} }
log.Printf("Updated currency rates: %d currencies cached", len(allCurrencies)) printDebug("Updated currency rates: %d currencies cached", len(allCurrencies))
return nil return nil
} }
@ -80,27 +80,31 @@ func PrecacheAllCurrencyPairs() {
return return
} }
log.Printf("Starting precache of all currency pairs (%d currencies)", len(allCurrencies)) printDebug("Precaching all currency pairs (%d total)", len(exchangeRates))
// We'll pre-cache the most common pairs directly for from := range exchangeRates {
commonCurrencies := []string{"USD", "EUR", "GBP", "JPY", "CAD", "AUD", "CHF", "CNY", "INR"} for to := range exchangeRates {
for _, from := range commonCurrencies {
for _, to := range allCurrencies {
if from == to { if from == to {
continue continue
} }
// Cache the cross-rate
GetExchangeRate(from, to) GetExchangeRate(from, to)
} }
} }
log.Println("Common currency pairs precached")
printDebug("All currency pairs precached")
} }
// GetExchangeRate gets the current exchange rate with caching // GetExchangeRate gets the current exchange rate with caching
func GetExchangeRate(from, to string) (float64, bool) { func GetExchangeRate(from, to string) (float64, bool) {
// Auto-update cache if expired // Auto-update cache if expired
if time.Since(lastUpdated) > cacheExpiration { if time.Now().After(nextUpdateTime) {
UpdateExchangeRates() // Ignore errors, use stale data if needed err := UpdateExchangeRates()
if err != nil {
printWarn("Currency update failed: %v", err)
// Postpone next attempt to avoid hammering API
nextUpdateTime = time.Now().Add(5 * time.Minute)
}
} }
exchangeCacheMutex.RLock() exchangeCacheMutex.RLock()