From 39200fb7632ec63ff7bdd61202a9e828d976c0fe Mon Sep 17 00:00:00 2001 From: tomwilsonsb <100102910+tomwilsonsb@users.noreply.github.com> Date: Wed, 31 May 2023 17:19:35 +1000 Subject: [PATCH 1/3] Add support for customising the redis key prefix --- lib/phoenix_pubsub_redis/redis.ex | 4 ++++ lib/phoenix_pubsub_redis/redis_server.ex | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/phoenix_pubsub_redis/redis.ex b/lib/phoenix_pubsub_redis/redis.ex index 923e9dd..f643ead 100644 --- a/lib/phoenix_pubsub_redis/redis.ex +++ b/lib/phoenix_pubsub_redis/redis.ex @@ -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 `nil` """ @@ -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) @@ -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 = [ diff --git a/lib/phoenix_pubsub_redis/redis_server.ex b/lib/phoenix_pubsub_redis/redis_server.ex index 3763a6a..a9a0ff6 100644 --- a/lib/phoenix_pubsub_redis/redis_server.ex +++ b/lib/phoenix_pubsub_redis/redis_server.ex @@ -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) @@ -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 @@ -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 @@ -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 From 9a557e540cb835b88742db5b1eebec2e8847b3f4 Mon Sep 17 00:00:00 2001 From: tomwilsonsb <100102910+tomwilsonsb@users.noreply.github.com> Date: Wed, 31 May 2023 17:25:42 +1000 Subject: [PATCH 2/3] Update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ca84f5a..9247f6d 100644 --- a/README.md +++ b/README.md @@ -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: From c522633997d0ab3ac17abc2e164bea4aad404fa3 Mon Sep 17 00:00:00 2001 From: tomwilsonsb <100102910+tomwilsonsb@users.noreply.github.com> Date: Thu, 1 Jun 2023 10:28:34 +1000 Subject: [PATCH 3/3] Update comment --- lib/phoenix_pubsub_redis/redis.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/phoenix_pubsub_redis/redis.ex b/lib/phoenix_pubsub_redis/redis.ex index f643ead..e49c38c 100644 --- a/lib/phoenix_pubsub_redis/redis.ex +++ b/lib/phoenix_pubsub_redis/redis.ex @@ -28,7 +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 `nil` + * `:key_prefix` - Key prefix to add to the redis PubSub key. Defaults `phx` """