CacheMoney (Cache Money v0.6.2) 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

Specs

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.

Specs

key() :: binary() | atom()

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

Specs

lazy_function() ::
  (() -> {:ok, value()}
         | {:ok, value(), integer()}
         | {:error, value()}
         | value())

Specs

options() :: [{:timeout, integer()}]

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

Specs

server() :: GenServer.server()

Specs

value() :: term()

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(server, key, opts \\ [])

View Source

Specs

delete(server(), key(), options()) :: {:ok, value()} | {:error, term()}

Deletes the key from the cache

Link to this function

get(server, key, opts \\ [])

View Source

Specs

get(server(), 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(server, key, fun, expiry \\ nil, opts \\ [])

View Source

Specs

get_lazy(server(), key(), lazy_function(), integer() | nil, options()) ::
  {:ok, value()} | {:error, any()}

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.

Specs

set(server(), key(), value()) :: {:ok, value()} | {:error, any()}

Sets key in the cache to value

Link to this function

set(server, key, value, opts)

View Source

Specs

set(server(), key(), value(), options()) :: {:ok, value()} | {:error, any()}

Sets key in the cache to value

Link to this function

set(server, key, value, expiry, opts \\ [])

View Source

Specs

set(server(), 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

Specs

start_link(cache_name(), map(), Keyword.t()) :: GenServer.on_start()

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