Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msysyamamoto/5b3340cb22aebf8436d8 to your computer and use it in GitHub Desktop.
Save msysyamamoto/5b3340cb22aebf8436d8 to your computer and use it in GitHub Desktop.
Poolboy Sample on Phoenix Framework
defmodule HelloPhoenix.Memcached.Supervisor do
use Supervisor
def start_link do
Supervisor.start_link(__MODULE__, [])
end
def init([]) do
pool_options = [
name: {:local, :memcached_pool},
worker_module: :mcd,
size: 5,
max_overflow: 0,
]
arg = ['localhost', 11211]
children = [
:poolboy.child_spec(:memcached_pool, pool_options, arg)
]
supervise(children, strategy: :one_for_one)
end
end
defmodule HelloPhoenix do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Start the endpoint when the application starts
supervisor(HelloPhoenix.Endpoint, []),
# Here you could define other workers and supervisors as children
# worker(HelloPhoenix.Worker, [arg1, arg2, arg3]),
supervisor(HelloPhoenix.Memcached.Supervisor, []),
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html
# for other strategies and supported options
opts = [strategy: :one_for_one, name: HelloPhoenix.Supervisor]
Supervisor.start_link(children, opts)
end
# Tell Phoenix to update the endpoint configuration
# whenever the application is updated.
def config_change(changed, _new, removed) do
HelloPhoenix.Endpoint.config_change(changed, removed)
:ok
end
end
defmodule HelloPhoenix.Mixfile do
use Mix.Project
def project do
[app: :hello_phoenix,
version: "0.0.1",
elixir: "~> 1.0",
elixirc_paths: elixirc_paths(Mix.env),
compilers: [:phoenix] ++ Mix.compilers,
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Configuration for the OTP application
#
# Type `mix help compile.app` for more information
def application do
[mod: {HelloPhoenix, []},
applications: [:phoenix, :phoenix_html, :cowboy, :logger, :poolboy]]
end
# Specifies which paths to compile per environment
defp elixirc_paths(:test), do: ["lib", "web", "test/support"]
defp elixirc_paths(_), do: ["lib", "web"]
# Specifies your project dependencies
#
# Type `mix help deps` for examples and options
defp deps do
[{:phoenix, "~> 1.0.0"},
{:phoenix_html, "~> 2.1"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:poolboy, "~> 1.5.1"},
{:mcd, github: "EchoTeam/mcd"},
{:cowboy, "~> 1.0"}]
end
end
defmodule HelloPhoenix.PageController do
use HelloPhoenix.Web, :controller
def index(conn, _params) do
# :poolboy.checkout/2, :poolboy.checkin/2 よりも :poolboy.transaction/2 の方がよさげ?
val = :poolboy.transaction(:memcached_pool, fn worker ->
:mcd.set(worker, "key", "Hello Phoenix")
:mcd.get(worker, "key")
|> case do
{:ok, resp} ->
resp
_ ->
"Boom!"
end
end)
json conn, %{val: val}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment