Improved IA currency caching
Some checks failed
Run Integration Tests / test (push) Failing after 40s
Some checks failed
Run Integration Tests / test (push) Failing after 40s
This commit is contained in:
parent
67eefc2c7a
commit
7dbdcbe202
1 changed files with 19 additions and 15 deletions
|
@ -16,9 +16,8 @@ import (
|
|||
// ExchangeRateCache holds currency rates with automatic refresh
|
||||
var (
|
||||
exchangeRates = make(map[string]float64)
|
||||
lastUpdated time.Time
|
||||
nextUpdateTime time.Time
|
||||
exchangeCacheMutex sync.RWMutex
|
||||
cacheExpiration = 1 * time.Hour
|
||||
allCurrencies []string
|
||||
)
|
||||
|
||||
|
@ -45,8 +44,9 @@ func UpdateExchangeRates() error {
|
|||
}
|
||||
|
||||
var apiResponse struct {
|
||||
Result string `json:"result"`
|
||||
Rates map[string]float64 `json:"rates"`
|
||||
Result string `json:"result"`
|
||||
Rates map[string]float64 `json:"rates"`
|
||||
TimeNextUpdateUnix int64 `json:"time_next_update_unix"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &apiResponse); err != nil {
|
||||
return err
|
||||
|
@ -58,7 +58,7 @@ func UpdateExchangeRates() error {
|
|||
|
||||
// Cache the rates
|
||||
exchangeRates = apiResponse.Rates
|
||||
lastUpdated = time.Now()
|
||||
nextUpdateTime = time.Unix(apiResponse.TimeNextUpdateUnix, 0)
|
||||
|
||||
// Update list of all currencies
|
||||
allCurrencies = make([]string, 0, len(exchangeRates))
|
||||
|
@ -66,7 +66,7 @@ func UpdateExchangeRates() error {
|
|||
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
|
||||
}
|
||||
|
||||
|
@ -80,27 +80,31 @@ func PrecacheAllCurrencyPairs() {
|
|||
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
|
||||
commonCurrencies := []string{"USD", "EUR", "GBP", "JPY", "CAD", "AUD", "CHF", "CNY", "INR"}
|
||||
|
||||
for _, from := range commonCurrencies {
|
||||
for _, to := range allCurrencies {
|
||||
for from := range exchangeRates {
|
||||
for to := range exchangeRates {
|
||||
if from == to {
|
||||
continue
|
||||
}
|
||||
// Cache the cross-rate
|
||||
GetExchangeRate(from, to)
|
||||
}
|
||||
}
|
||||
log.Println("Common currency pairs precached")
|
||||
|
||||
printDebug("All currency pairs precached")
|
||||
}
|
||||
|
||||
// GetExchangeRate gets the current exchange rate with caching
|
||||
func GetExchangeRate(from, to string) (float64, bool) {
|
||||
// Auto-update cache if expired
|
||||
if time.Since(lastUpdated) > cacheExpiration {
|
||||
UpdateExchangeRates() // Ignore errors, use stale data if needed
|
||||
if time.Now().After(nextUpdateTime) {
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue