Forex.Cache behaviour (forex v0.1.1)

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. You can override the cache module by setting the :cache_module configuration option in the Forex.Fetcher module options.

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(:current_rates, {Forex.Feed, :fetch_current_rates, []})

terminate()

@callback terminate() :: any()

Terminate the cache process.

Functions

cache_mod()

current_rates()

delete(key)

historic_rates()

init()

initialized?()

last_ninety_days_rates()

last_updated()

last_updated(key)

reset()

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

terminate()