Cache Money v0.5.2 CacheMoney View Source

Handles caching values under different cache names, can expire keys

Link to this section Summary

Types

The name of the cache, used for namespacing multiple caches on the same adapter. Can be either a binary or an atom, but will always be converted to a binary

The key a value will be set under. Can be either a binary or an atom, but will always be converted to a binary

Currently the only option available is an optional timeout that gets passed along with GenServer.call

The value to be saved in the cache. Can be any value going in to the cache, but depending on the adapter used, may not be the same value going out. For example, CacheMoney.Adapters.ETS can save any elixir term, including pids. CacheMoney.Adapters.Redis, however, can only save items as strings

Functions

Returns a specification to start this module under a supervisor

Deletes the key from the cache

Gets the value out of the cache using the key

Gets the value out of the cache using the key. Lazily fetches the data, inserts it into the cache, and returns it if it does not exist. Optional expiry is in seconds

Sets key in the cache to value

Sets key in the cache to value

Sets key in the cache to value, which expires after expiry seconds

Starts a CacheMoney process linked to the current process

Link to this section Types

Link to this type cache_name() View Source
cache_name() :: binary() | atom()

The name of the cache, used for namespacing multiple caches on the same adapter. Can be either a binary or an atom, but will always be converted to a binary.

The key a value will be set under. Can be either a binary or an atom, but will always be converted to a binary.

Link to this type options() View Source
options() :: [{:timeout, integer()}]

Currently the only option available is an optional timeout that gets passed along with GenServer.call

The value to be saved in the cache. Can be any value going in to the cache, but depending on the adapter used, may not be the same value going out. For example, CacheMoney.Adapters.ETS can save any elixir term, including pids. CacheMoney.Adapters.Redis, however, can only save items as strings.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function delete(pid, key, opts \\ []) View Source
delete(pid(), key(), options()) :: {:ok, value()} | {:error, term()}

Deletes the key from the cache

Link to this function get(pid, key, opts \\ []) View Source
get(pid(), key(), options()) :: {:ok, value()} | {:error, term()}

Gets the value out of the cache using the key.

If the value does not exist in the cache nil will be returned.

Link to this function get_lazy(pid, key, fun, expiry \\ nil, opts \\ []) View Source

Gets the value out of the cache using the key. Lazily fetches the data, inserts it into the cache, and returns it if it does not exist. Optional expiry is in seconds.

Link to this function set(pid, key, value) View Source
set(pid(), key(), value()) :: {:ok, value()} | {:error, any()}

Sets key in the cache to value

Link to this function set(pid, key, value, opts) View Source
set(pid(), key(), value(), options()) :: {:ok, value()} | {:error, any()}

Sets key in the cache to value

Link to this function set(pid, key, value, expiry, opts \\ []) View Source
set(pid(), key(), (() -> value()), integer(), options()) ::
  {:ok, value()} | {:error, any()}
set(pid(), key(), value(), integer(), options()) ::
  {:ok, value()} | {:error, any()}

Sets key in the cache to value, which expires after expiry seconds

Link to this function start_link(cache, config, opts \\ []) View Source
start_link(cache_name(), %{}, Keyword.t()) :: pid()

Starts a CacheMoney process linked to the current process.

Arguments

  • cache - the name of the cache. Multiple caches using the same adapter will all be in the same spot, but will be namespaced by the given cache name.
  • conifg - contains various configuration options for the cache, depending on the adapter. :adapter is required to be set, and must be set to a module that implements CacheMoney.Adapter, such as CacheMoney.Adapters.Redis or CacheMoney.Adapters.ETS. Different adapters will also specify other required optionso be passed to them through the config argument
  • opts - see GenServer.start_link/3. Options are passed straight through to the underlying GenServer