class ExchangerLocal
used for local cache
Constants
- EXPIRE_DURATION
properties
Public Class Methods
new()
click to toggle source
default constructor
Calls superclass method
ExchangerBase::new
# File lib/mrpin/core/currency_exchanger/local/exchanger_local.rb, line 16 def initialize super @locker = Mutex.new #key - from + to, value - hash {timestamp_updated_at: rate: } @rates_map = {} end
Public Instance Methods
get_rate(from_currency, to_currency)
click to toggle source
# File lib/mrpin/core/currency_exchanger/local/exchanger_local.rb, line 25 def get_rate(from_currency, to_currency) result = nil @locker.synchronize do key = "#{from_currency}_#{to_currency}" data = @rates_map[key] unless data.nil? updated_at = data[:updated_at] rate = data[:rate] if Time.now.to_i - updated_at > EXPIRE_DURATION @rates_map.delete(key) else result = rate end #check time end #check data end #locker result end
set_rate(from_currency, to_currency, rate)
click to toggle source
# File lib/mrpin/core/currency_exchanger/local/exchanger_local.rb, line 49 def set_rate(from_currency, to_currency, rate) return if rate == 0 || rate.nil? @locker.synchronize do set_rate_unsafe(from_currency, to_currency, rate) set_rate_unsafe(to_currency, from_currency, 1.to_f / rate) end nil end
Private Instance Methods
set_rate_unsafe(from_currency, to_currency, rate)
click to toggle source
# File lib/mrpin/core/currency_exchanger/local/exchanger_local.rb, line 61 def set_rate_unsafe(from_currency, to_currency, rate) key = "#{from_currency}_#{to_currency}" data = { updated_at: Time.now.to_i, rate: rate } @rates_map[key] = data nil end