Defines the cache behaviour for exchange rates.
Most callbacks receive a cache/0 term - the value returned by init/1 -
identifying which retriever's storage to operate on. This allows a single
cache module (such as the bundled Money.ExchangeRates.Cache.Ets and
Money.ExchangeRates.Cache.Dets) to be shared safely by multiple named
retrievers, each backed by its own separated storage.
Migrating from the deprecated singleton callbacks
Older cache modules implemented init/0, terminate/0, latest_rates/0,
historic_rates/1, last_updated/0, store_latest_rates/2 and
store_historic_rates/2, storing rates as fixed, module-wide state. These
still work, but named retrievers sharing such a module overwrite each other's
rates.
To migrate, add the cache/0 value returned by init/1 as the leading
argument to every other callback, and key storage off the name given to
init/1 instead of a fixed identifier:
def init(name), do: :ets.new(name, [:named_table, :public])
def latest_rates(cache), do: :ets.lookup(cache, :latest_rates)
def store_latest_rates(cache, rates, retrieved_at) do
:ets.insert(cache, {:latest_rates, rates})
:ets.insert(cache, {:last_updated, retrieved_at})
endterminate/1, historic_rates/2 and store_historic_rates/3 follow the
same pattern.
Summary
Callbacks
Retrieve the exchange rates for a given date.
Initialize the cache when the exchange rates retriever is started
Return the timestamp when the exchange rates were last updated.
Retrieve the latest exchange rates from the cache.
Store the historic exchange rates for a given date in the cache.
Store the latest exchange rates in the cache.
Terminate the cache when the retriever process stops normally
Types
Callbacks
@callback historic_rates(Date.t()) :: {:ok, Money.ExchangeRates.t()} | {:error, {Exception.t(), String.t()}}
@callback historic_rates(cache(), Date.t()) :: {:ok, Money.ExchangeRates.t()} | {:error, {Exception.t(), String.t()}}
Retrieve the exchange rates for a given date.
@callback init() :: any()
Initialize the cache when the exchange rates retriever is started
Called with the retriever's :name, so that a cache module can maintain
separate storage per retriever. Must return a cache/0 value that is
passed to every other callback.
@callback last_updated() :: {:ok, DateTime.t()} | {:error, {Exception.t(), String.t()}}
@callback last_updated(cache()) :: {:ok, DateTime.t()} | {:error, {Exception.t(), String.t()}}
Return the timestamp when the exchange rates were last updated.
@callback latest_rates() :: {:ok, Money.ExchangeRates.t()} | {:error, {Exception.t(), String.t()}}
@callback latest_rates(cache()) :: {:ok, Money.ExchangeRates.t()} | {:error, {Exception.t(), String.t()}}
Retrieve the latest exchange rates from the cache.
@callback store_historic_rates(Money.ExchangeRates.t(), Date.t()) :: :ok
@callback store_historic_rates(cache(), Money.ExchangeRates.t(), Date.t()) :: :ok
Store the historic exchange rates for a given date in the cache.
@callback store_latest_rates(Money.ExchangeRates.t(), DateTime.t()) :: :ok
@callback store_latest_rates(cache(), Money.ExchangeRates.t(), DateTime.t()) :: :ok
Store the latest exchange rates in the cache.
@callback terminate() :: any()
Terminate the cache when the retriever process stops normally