Operate v0.1.0-beta.15 Operate.Cache behaviour View Source
Operate cache specification.
A cache is responsible for storing and retrieving tapes and ops from a cache, and if necessary instructing an adapter to fetch items from a data source.
Operate comes bundled with a ConCache
ETS cache, although by default runs
without any caching.
Creating a cache
A cache must implement both of the following callbacks:
fetch_tx/3
- function that takes a txid and returns aOperate.BPU.Transaction.t/0
fetch_ops/3
- function that takes a list of Op references and returns a list ofOperate.Op.t/0
functions.
The third argument in both functions is a tuple containing the adapter module and a keyword list of options to pass to the adapter.
defmodule MyCache do
use Operate.Cache
def fetch_tx(txid, opts, {adapter, adapter_opts}) do
ttl = Keyword.get(opts, :ttl, 3600)
Cache.fetch_or_store(txid, ttl: ttl, fn ->
adapter.fetch_tx(txid, adapter_opts)
end)
end
end
Using the above example, Operate can be configured with:
{Operate, [
cache: {MyCache, [ttl: 3600]}
]}
Link to this section Summary
Callbacks
Loads Ops from the cache by the given procedure referneces, or delegates
the job to the specified adapter. Returns the result in an :ok
/ :error
tuple pair.
As fetch_ops/3
, but returns the result or raises an exception.
Loads a transaction from the cache by the given txid, or delegates to job to
the specified adapter. Returns the result in an :ok
/ :error
tuple pair.
As fetch_tx/3
, but returns the transaction or raises an exception.
Loads a list of transactions from the cache by the given query map, or
delegates to job to the specified adapter. Returns the result in an
:ok
/ :error
tuple pair.
As fetch_tx_by/3
, but returns the result or raises an exception.
Link to this section Callbacks
Specs
fetch_ops(list(), keyword(), {module(), keyword()}) :: {:ok, [Operate.Op.t(), ...]} | {:error, String.t()}
Loads Ops from the cache by the given procedure referneces, or delegates
the job to the specified adapter. Returns the result in an :ok
/ :error
tuple pair.
Specs
fetch_ops!(list(), keyword(), {module(), keyword()}) :: [Operate.Op.t(), ...]
As fetch_ops/3
, but returns the result or raises an exception.
Specs
fetch_tx(String.t(), keyword(), {module(), keyword()}) :: {:ok, Operate.Tape.t()} | {:error, String.t()}
Loads a transaction from the cache by the given txid, or delegates to job to
the specified adapter. Returns the result in an :ok
/ :error
tuple pair.
Specs
fetch_tx!(String.t(), keyword(), {module(), keyword()}) :: Operate.Tape.t()
As fetch_tx/3
, but returns the transaction or raises an exception.
Specs
fetch_tx_by(map(), keyword(), {module(), keyword()}) :: {:ok, [Operate.Tape.t(), ...]} | {:error, String.t()}
Loads a list of transactions from the cache by the given query map, or
delegates to job to the specified adapter. Returns the result in an
:ok
/ :error
tuple pair.
Specs
fetch_tx_by!(map(), keyword(), {module(), keyword()}) :: [Operate.Tape.t(), ...]
As fetch_tx_by/3
, but returns the result or raises an exception.