Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key_prefix option to change the key named used in redis #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Option | Description
`:password` | The redis-server password | `""` |
`:compression_level` | Compression level applied to serialized terms (`0` - none, `9` - highest) | `0` |
`:socket_opts` | The redis-server network layer options | `[]` |
`:key_prefix` | The key prefix to use for the redis key | `phx` |

And also add `:phoenix_pubsub_redis` to your list of applications:

Expand Down
4 changes: 4 additions & 0 deletions lib/phoenix_pubsub_redis/redis.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ defmodule Phoenix.PubSub.Redis do
* `:compression_level` - Compression level applied to serialized terms - from `0` (no compression), to `9` (highest). Defaults `0`
* `:socket_opts` - List of options that are passed to the network layer when connecting to the Redis server. Default `[]`
* `:sentinel` - Redix sentinel configuration. Default to `nil`
* `:key_prefix` - Key prefix to add to the redis PubSub key. Defaults `phx`

"""

Expand Down Expand Up @@ -66,6 +67,7 @@ defmodule Phoenix.PubSub.Redis do
pubsub_name = Keyword.fetch!(opts, :name)
adapter_name = Keyword.fetch!(opts, :adapter_name)
compression_level = Keyword.get(opts, :compression_level, 0)
key_prefix = Keyword.get(opts, :key_prefix, nil)

opts = handle_url_opts(opts)
opts = Keyword.merge(@defaults, opts)
Expand All @@ -74,8 +76,10 @@ defmodule Phoenix.PubSub.Redis do
node_name = opts[:node_name] || node()
validate_node_name!(node_name)


:ets.new(adapter_name, [:public, :named_table, read_concurrency: true])
:ets.insert(adapter_name, {:node_name, node_name})
:ets.insert(adapter_name, {:key_prefix, key_prefix})
:ets.insert(adapter_name, {:compression_level, compression_level})

pool_opts = [
Expand Down
18 changes: 15 additions & 3 deletions lib/phoenix_pubsub_redis/redis_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Phoenix.PubSub.RedisServer do
end

defp publish(adapter_name, mode, node_name, topic, message, dispatcher) do
namespace = redis_namespace(adapter_name)
namespace = redis_namespace(adapter_name, key_prefix(adapter_name))
compression_level = compression_level(adapter_name)
redis_msg = {@redis_msg_vsn, mode, node_name, topic, message, dispatcher}
bin_msg = :erlang.term_to_binary(redis_msg, compressed: compression_level)
Expand All @@ -46,6 +46,10 @@ defmodule Phoenix.PubSub.RedisServer do
:ets.lookup_element(adapter_name, :compression_level, 2)
end

def key_prefix(adapter_name) do
:ets.lookup_element(adapter_name, :key_prefix, 2)
end

## Server callbacks

def start_link({_, adapter_name, _, _} = state) do
Expand Down Expand Up @@ -125,7 +129,13 @@ defmodule Phoenix.PubSub.RedisServer do
end

defp establish_success(%{redix_pid: redix_pid, adapter_name: adapter_name} = state) do
{:ok, _reference} = Redix.PubSub.subscribe(redix_pid, redis_namespace(adapter_name), self())
{:ok, _reference} =
Redix.PubSub.subscribe(
redix_pid,
redis_namespace(adapter_name, key_prefix(adapter_name)),
self()
)

state
end

Expand All @@ -139,5 +149,7 @@ defmodule Phoenix.PubSub.RedisServer do
end
end

defp redis_namespace(adapter_name), do: "phx:#{adapter_name}"
defp redis_namespace(adapter_name, prefix)
defp redis_namespace(adapter_name, nil), do: "phx:#{adapter_name}"
defp redis_namespace(adapter_name, key_prefix), do: "#{key_prefix}:#{adapter_name}"
end