Forex.Cache behaviour (Forex v1.1.3)

Copy Markdown View Source

Since the ECB reference rates are usually updated at around 16:00 CET every working day we should cache the response.

This module defines a simple caching layer behaviour to cache the responses from the Forex.Feed module.

The default implementation uses ETS as the cache storage (Forex.Cache.ETS adapter). You can override the cache module by setting the :cache_module configuration option, but it must implement the Forex.Cache behaviour. For example, to use a custom cache module, you can set the configuration like this:

config :forex, cache_module: MyApp.ForexCache

There is also a Forex.Cache.DETS adapter that uses DETS for persistent storage, which can be useful for testing or if you want to persist the cache across application restarts. Take into consideration that when using DETS, the path to the cache file must be a writable directory and the file will be created if it does not exist.

Summary

Callbacks

Delete an entry from the cache with the given key.

Get the cache entry with the given key. Returns nil if the entry does not exist.

Inits the cache.

Check if the cache is initialized.

Get a list of all keys in the cache and the respective updated at timestamps.

Get the latest updated at timestamp for the given key. Returns nil if the entry does not exist.

Put a new entry into the cache with the given key and value.

Reset the cache table.

Resolve the cache entry with the given key using the given resolver. If the entry does not exist, the resolver function, an mfa tuple, is called and the result is stored in the cache.

Terminate the cache process.

Callbacks

delete(key)

@callback delete(key :: any()) :: term()

Delete an entry from the cache with the given key.

get(key, opts)

@callback get(key :: any(), opts :: Keyword.t()) :: term()

Get the cache entry with the given key. Returns nil if the entry does not exist.

init()

@callback init() :: any()

Inits the cache.

initialized?()

@callback initialized?() :: boolean()

Check if the cache is initialized.

last_updated()

@callback last_updated() :: [{any(), DateTime.t()}] | nil

Get a list of all keys in the cache and the respective updated at timestamps.

last_updated(key)

@callback last_updated(key :: any()) :: DateTime.t() | nil

Get the latest updated at timestamp for the given key. Returns nil if the entry does not exist.

put(key, value, t)

@callback put(key :: any(), value :: term(), DateTime.t()) :: term()

Put a new entry into the cache with the given key and value.

reset()

@callback reset() :: any()

Reset the cache table.

resolve(key, resolver, opts)

@callback resolve(key :: any(), resolver :: mfa() | function(), opts :: Keyword.t()) ::
  term()

Resolve the cache entry with the given key using the given resolver. If the entry does not exist, the resolver function, an mfa tuple, is called and the result is stored in the cache.

Example:

Forex.Cache.resolve(:latest_rates, {Forex.Feed, :fetch_latest_rates, []})

terminate()

@callback terminate() :: any()

Terminate the cache process.

Functions

cache_mod()

delete(key)

historic_rates()

init()

initialized?()

last_ninety_days_rates()

last_updated()

last_updated(key)

latest_rates()

reset()

resolve(key, resolver, opts \\ [])

terminate()