cream v0.2.0 Cream.Cluster behaviour

Connect to a cluster of memcached servers (or a single server if you want).

{:ok, cluster} = Cream.Cluster.start_link(servers: ["host1:11211", "host2:11211"])
Cream.Cluster.get(cluster, "foo")

Using a module and Mix.Config is preferred…

# In config/*.exs

use Mix.Config
config :my_app, MyCluster,
  servers: ["host1:11211", "host2:11211"]

# Elsewhere

defmodule MyCluster do
  use Cream.Cluster, otp_app: :my_app
end

{:ok, _} = MyCluster.start_link
MyCluster.get("foo")

Link to this section Summary

Types

Configuration options

A key value pair

Multiple items as a list of tuples or a map

A memcached key

A list of keys

Reason associated with an error

Anything serializable (to JSON)

t()

Type representing a Cream.Cluster

A value to be stored in memcached

Functions

Delete one or more keys

Fetch one or more keys, falling back to a function if a key doesn’t exist

Flush all memcached servers in the cluster

Set the value of a single key

Connect to memcached server(s)

Get Memcache connection(s) for one or more keys

Link to this section Types

Link to this type config()
config() :: Keyword.t

Configuration options.

Link to this type item()
item() :: {key, value}

A key value pair.

Link to this type items()
items() :: [item] | [%{required(key) => value}]

Multiple items as a list of tuples or a map.

A memcached key.

Link to this type keys()
keys() :: [key]

A list of keys.

Link to this type memcache_connection()
memcache_connection() :: GenServer.server

A Memcache.Connection.

Link to this type reason()
reason() :: String.t

Reason associated with an error.

Link to this type serializable()
serializable() :: list | map

Anything serializable (to JSON).

Type representing a Cream.Cluster.

A value to be stored in memcached.

Link to this section Functions

Link to this function delete(cluster, key_or_keys)
delete(t, keys) :: %{required(key) => :ok | {:error, reason}}
delete(t, key) :: :ok | {:error, reason}

Delete one or more keys.

Examples

delete("foo")
delete(["foo", "one"])
Link to this function fetch(cluster, key_or_keys, opts \\ [], func)
fetch(t, keys, Keyword.t, (keys -> [value] | items)) :: items
fetch(t, key, Keyword.t, (() -> value)) :: value

Fetch one or more keys, falling back to a function if a key doesn’t exist.

opts is the same as for set/3.

Ex:

fetch(cluster, "foo", fn -> "bar" end)

fetch(cluster, ["foo", "bar"], fn missing_keys ->
  Enum.map(missing_keys, fn missing_key ->
    calc_value(missing_key)
  end)
end)

# In this example, we explicitly associate missing keys with values.
fetch(cluster, ["foo", "bar"], fn missing_keys ->
  Enum.shuffle(missing_keys)
    |> Enum.map(fn missing_key ->
      {missing_key, calc_value(missing_key)}
    end)
end)
Link to this function flush(cluster, opts \\ [])
flush(t, Keyword.t) :: [:ok | {:error, reason}]

Flush all memcached servers in the cluster.

Link to this function get(cluster, key_or_keys, opts \\ [])
get(t, keys, Keyword.t) :: items
get(t, key, Keyword.t) :: value

Get one or more keys.

Examples

"bar" = get(pid, "foo")
%{"foo" => "bar", "one" => "one"} = get(pid, ["foo", "bar"])
Link to this function put(cluster, key, value, opts \\ [])
put(t, key, value, Keyword.t) :: :ok | {:error, reason}

Set the value of a single key.

This a convenience function for set(cluster, {key, value}, opts). See set/3.

It has a different name because the follow definitions conflict:

set(cluster, key, value, opts \\ [])
set(cluster, item, opts \\ [])
Link to this function set(cluster, item_or_items, opts \\ [])
set(t, item | items, Keyword.t) :: :ok | {:error, reason}

Set one or more keys.

Single key examples:

set(cluster, {key, value})
set(cluster, {key, value}, ttl: 300)

Multiple key examples:

set(cluster, [{k1, v1}, {k2, v2}])
set(cluster, [{k1, v1}, {k2, v2}], ttl: 300)

set(cluster, %{k1 => v1, k2 => v2})
set(cluster, %{k1 => v1, k2 => v2}, ttl: 300)
Link to this function start_link(opts \\ [])
start_link(Keyword.t) :: t

Connect to memcached server(s)

Options

  • :servers - Servers to connect to. Defaults to ["localhost:11211"].
  • :pool - Worker pool size. Defaults to 10.
  • :name - Like name argument for GenServer.start_link/3. No default. Ignored if using module based cluster.
  • :memcachex - Keyword list passed through to Memcache.start_link/2

Example

{:ok, cluster} = Cream.Cluster.start_link(
  servers: ["host1:11211", "host2:11211"],
  name: MyCluster,
  memcachex: [ttl: 60, namespace: "foo"]
)
Link to this function with_conn(cluster, key_or_keys, func)
with_conn(t, keys, (memcache_connection, keys -> any)) :: [any]
with_conn(t, key, (memcache_connection -> any)) :: any

Get Memcache connection(s) for one or more keys.

Memcachex doesn’t support clustering. Cream supports clustering, but doesn’t support the full memcached API. This function lets you do both.

The connection yielded to the function can be used with all of the Memcache and Memcache.Connection modules.

Examples

with_conn cluster, keys, fn conn, keys ->
  Memcache.multi_get(conn, keys)
end

Link to this section Callbacks

Link to this callback child_spec(config)
child_spec(config) :: Supervisor.child_spec

For easily putting into supervision tree.

Ex:

Supervisor.start_link([MyCluster], opts)

Or if you want to do runtime config here instead of the init/1 callback for some reason:

Supervisor.start_link([{MyCluster, servers: servers}], opts)
Link to this callback delete(key_or_keys)
delete(key_or_keys :: key | keys) ::
  :ok | {:error, reason} |
  [{key, :ok | {:error, reason}}]

See delete/2.

Link to this callback fetch(key_or_keys, f)
fetch(key_or_keys :: key | keys, f :: (() -> value) | (keys -> [value] | items)) ::
  value |
  items

See fetch/4.

Link to this callback flush(opts)
flush(opts :: Keyword.t) :: :ok | {:error, reason}

See flush/2.

Link to this callback get(key_or_keys)
get(key_or_keys :: key | keys) :: value | items

See get/2.

Link to this callback init(config)
init(config) :: {:ok, config} | {:error, reason}

For dynamic / runtime configuration.

Ex:

defmodule MyCluster do
  use Cream.Cluster, otp_app: :my_app

  def init(config) do
    servers = System.get_env("MEMCACHED_SERVERS") |> String.split(",")
    config = Keyword.put(config, :servers, servers)
    {:ok, config}
  end
end
Link to this callback put(key, value, opts)
put(key, value, opts :: Keyword.t) :: :ok | {:error, reason}

See put/4.

Link to this callback set(item_or_items, opts)
set(item_or_items :: item | items, opts :: Keyword.t) ::
  :ok |
  {:error, reason} |
  %{required(key) => :ok | {:error | reason}}

See set/3.