Skip to content

peillis/memcachir

Repository files navigation

Memcachir

Build Status Module Version Hex Docs Total Download License Last Updated

Memcached client for Elixir. It supports clusters and AWS Elasticache.

Installation

defp deps() do
  ...
  {:memcachir, "~> 3.3"},
  ...
end

defp application() do
  [applications: [:logger, :memcachir, ...]]
end
config :memcachir,
  hosts: "localhost"

The hosts config allows multiple variants:

hosts: "localhost:11212"  # specify port
hosts: ["host1", "host2", "host3:11212"]  # cluster of servers
hosts: [{"host1", 10}, {"host2", 30}]  # cluster with weights

Alternatively you can use the elasticache config option:

config :memcachir,
  elasticache: "your-config-endpoint.cache.amazonaws.com"

Configuration

Complete configuration options with default values:

config :memcachir,
  hosts: "localhost",
  # memcached options
  ttl: 0,
  namespace: nil,
  # connection pool options
  pool: [
    strategy: :lifo,
    size: 10,
    max_overflow: 10]

Service Discovery

If you don't want to use the built in service discovery methods (host list, elasticache), you can implement the Herd.Discovery behavior, which just has a single nodes/0 callback. Then configure it in with:

config :memcachir, :service_discovery, MyMemcacheServiceDiscovery

(NB you'll need to delete the config :memcachir, :hosts and config :memcachir, :elasticache entries to use a custom service discovery module)

Example

iex> Memcachir.set("hello", "world")
{:ok}
iex> Memcachir.get("hello")
{:ok, "world"}

Example with ttl (in seconds)

iex> Memcachir.set("hello", "world", ttl: 5)
{:ok}
iex> Memcachir.get("hello")
{:ok, "world"}
iex> :timer.sleep(5001)
:ok
iex> Memcachir.get("hello")
{:error, "Key not found"}

Copyright and License

Copyright (c) 2017 Enrique Martinez

This library licensed under the MIT license.