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)
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
Get one or more keys
Set the value of a single key
Set one or more keys
Connect to memcached server(s)
Get Memcache
connection(s) for one or more keys
Link to this section 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).
Type representing a Cream.Cluster
.
A value to be stored in memcached.
Link to this section Functions
Delete one or more keys.
Examples
delete("foo")
delete(["foo", "one"])
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)
Flush all memcached servers in the cluster.
Get one or more keys.
Examples
"bar" = get(pid, "foo")
%{"foo" => "bar", "one" => "one"} = get(pid, ["foo", "bar"])
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 \\ [])
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)
Connect to memcached server(s)
Options
:servers
- Servers to connect to. Defaults to["localhost:11211"]
.:pool
- Worker pool size. Defaults to10
.:name
- Like name argument forGenServer.start_link/3
. No default. Ignored if using module based cluster.:memcachex
- Keyword list passed through toMemcache.start_link/2
Example
{:ok, cluster} = Cream.Cluster.start_link(
servers: ["host1:11211", "host2:11211"],
name: MyCluster,
memcachex: [ttl: 60, namespace: "foo"]
)
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
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)
See delete/2
.
See fetch/4
.
See flush/2
.
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
See put/4
.