Routemaster Client v0.3.0 Routemaster.Cache View Source

A persistent cache that survives application restarts, backed by Redis.

Link to this section Summary

Functions

It clears a key-value from the cache

Tries to read key from the cache, and returns the stored value if anything is found. If no vlaue is found in the cache, it executes the fallback function, caches the result, and returns the value in a tuple

Reads a key-value and returns an Elixr term, in a tuple

Writes a key-value and returns the written value, in a tuple

Link to this section Types

Link to this type fallback() View Source
fallback() :: (() -> any())

Link to this section Functions

Link to this function clear(key) View Source
clear(atom() | binary()) :: :ok | :error

It clears a key-value from the cache

iex> Routemaster.Cache.write(:mango, "so good")
{:ok, "so good"}
iex> Routemaster.Cache.clear(:mango)
:ok
iex> Routemaster.Cache.read(:mango)
{:miss, nil}
Link to this function fetch(key, fallback) View Source
fetch(atom() | binary(), fallback()) :: {:ok, any()} | {:error, any()}

Tries to read key from the cache, and returns the stored value if anything is found. If no vlaue is found in the cache, it executes the fallback function, caches the result, and returns the value in a tuple.

iex> Routemaster.Cache.read(:peach)
{:miss, nil}
iex> Routemaster.Cache.fetch(:peach, fn() -> "apricot" end)
{:ok, "apricot"}
iex> Routemaster.Cache.fetch(:peach, fn() -> "coconut" end)
{:ok, "apricot"}
iex> Routemaster.Cache.read(:peach)
{:ok, "apricot"}
Link to this function read(key) View Source
read(atom() | binary()) :: {:ok, any()} | {:miss, nil} | {:error, any()}

Reads a key-value and returns an Elixr term, in a tuple.

iex> Routemaster.Cache.read(:apple)
{:miss, nil}
iex> Routemaster.Cache.write(:apple, %{is: "a", good: "fruit"})
{:ok, %{is: "a", good: "fruit"}}
iex> Routemaster.Cache.read(:apple)
{:ok, %{is: "a", good: "fruit"}}
Link to this function write(key, term) View Source
write(atom() | binary(), any()) :: {:ok, any()} | {:error, any()}

Writes a key-value and returns the written value, in a tuple.

iex> Routemaster.Cache.write(:pear, [1, 2, 3])
{:ok, [1, 2, 3]}
iex> Routemaster.Cache.read(:pear)
{:ok, [1, 2, 3]}