This package is basically a wrapper around exredis with poolboy. The main goal was to be able to use all Exredis.Api functions which automatically use workers from the worker pool.
- Add
exredis_poolboy
to your list of dependencies inmix.exs
:
def deps do
[{:exredis_poolboy, "~> 0.2.0"}]
end
- Ensure
exredis_poolboy
is started before your application:
def application do
[applications: [:exredis_poolboy]]
end
- Add
ExredisPoolboy.PoolSupervisor
to your supervision tree and provide application config key as an argument:
children = [
supervisor(ExredisPoolboy.PoolSupervisor, [config_key: :my_app_redis])
]
- Create a module for using the exredis functions:
defmodule MyApp.Redis do
use ExredisPoolboy.FunctionsDefinitions, :my_app_redis
end
- Add config:
# The fields for redis matches the exredis configuration
config :my_app_redis, :redis,
host: "redis",
port: 6379,
password: "",
db: 0,
reconnect: :no_reconnect,
max_queue: :infinity
# Any name works here, used to create different connection pools if needed
config :my_app_redis, :pool_name, :my_app_redis_pool
# Optional, defaults to 10
config :my_app_redis, :pool, 20
- Use just like you would use exredis:
MyApp.Redis.llen("key")
MyApp.Redis.sadd("key", "value")
All functions are marked as overridable so you can extend basic functions, for example:
defmodule MyApp.Redis do
use ExredisPoolboy.FunctionsDefinitions, :my_app_redis
def llen(key) do
key
|> super()
|> String.to_integer()
end
end
- Add support for
only
/except
withuse ExredisPoolboy.FunctionsDefinitions
- Allow using functions on ExredisPoolboy module directly (without need to start separate supervisor), e.g.
ExredisPoolboy.llen("key")