mirror of
https://github.com/we-promise/sure.git
synced 2026-04-17 02:54:10 +00:00
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:
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user