Rate exchange fix (#377)

* FIX cache hit issue

* Change to proper method

* FIX cache is read heavy, so change to find
This commit is contained in:
soky srm
2025-11-24 17:54:18 +01:00
committed by GitHub
parent 0270363f0a
commit 4cd575fa23

View File

@@ -19,12 +19,23 @@ module ExchangeRate::Provided
return nil unless response.success? # Provider error
rate = response.data
ExchangeRate.find_or_create_by!(
from_currency: rate.from,
to_currency: rate.to,
date: rate.date,
rate: rate.rate
) if cache
begin
ExchangeRate.find_or_create_by!(
from_currency: rate.from,
to_currency: rate.to,
date: rate.date
) do |exchange_rate|
exchange_rate.rate = rate.rate
end if cache
rescue ActiveRecord::RecordNotUnique
# Race condition: another process inserted between our SELECT and INSERT
# Retry by finding the existing record
ExchangeRate.find_by!(
from_currency: rate.from,
to_currency: rate.to,
date: rate.date
) if cache
end
rate
end