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

Support tuples as keys for get_all/put_all callbacks in Redis/Client cluster #57

Merged
merged 1 commit into from
Jan 7, 2024
Merged
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
26 changes: 15 additions & 11 deletions lib/nebulex_redis_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ defmodule NebulexRedisAdapter do

defp do_get_all(adapter_meta, keys, opts) do
keys
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :keys)
|> Enum.reduce(%{}, fn {hash_slot, keys}, acc ->
return = mget(hash_slot, adapter_meta, keys, opts)

Expand Down Expand Up @@ -599,7 +599,7 @@ defmodule NebulexRedisAdapter do

_ ->
entries
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :tuples)
|> Enum.reduce(:ok, fn {hash_slot, group}, acc ->
acc && do_put_all(adapter_meta, hash_slot, group, ttl, on_write, opts)
end)
Expand Down Expand Up @@ -782,7 +782,7 @@ defmodule NebulexRedisAdapter do
when is_list(keys) do
:ok =
keys
|> group_keys_by_hash_slot(adapter_meta)
|> group_keys_by_hash_slot(adapter_meta, :keys)
|> Enum.each(fn {hash_slot, keys_group} ->
Command.exec!(
adapter_meta,
Expand Down Expand Up @@ -876,16 +876,20 @@ defmodule NebulexRedisAdapter do
apply(RedisCluster, :exec!, args ++ extra_args)
end

defp group_keys_by_hash_slot(enum, %{
mode: :client_side_cluster,
nodes: nodes,
keyslot: keyslot
}) do
ClientCluster.group_keys_by_hash_slot(enum, nodes, keyslot)
defp group_keys_by_hash_slot(
enum,
%{
mode: :client_side_cluster,
nodes: nodes,
keyslot: keyslot
},
enum_type
) do
ClientCluster.group_keys_by_hash_slot(enum, nodes, keyslot, enum_type)
end

defp group_keys_by_hash_slot(enum, %{mode: :redis_cluster, keyslot: keyslot}) do
RedisCluster.group_keys_by_hash_slot(enum, keyslot)
defp group_keys_by_hash_slot(enum, %{mode: :redis_cluster, keyslot: keyslot}, enum_type) do
RedisCluster.group_keys_by_hash_slot(enum, keyslot, enum_type)
end

defp enc_key(%{serializer: serializer, encode_key_opts: enc_key_opts}, key) do
Expand Down
18 changes: 6 additions & 12 deletions lib/nebulex_redis_adapter/client_cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,13 @@ defmodule NebulexRedisAdapter.ClientCluster do
Pool.get_conn(registry, {name, node_name}, pool_size)
end

@spec group_keys_by_hash_slot(Enum.t(), nodes_config, module) :: map
def group_keys_by_hash_slot(enum, nodes, module) do
Enum.reduce(enum, %{}, fn
{key, _} = entry, acc ->
hash_slot = hash_slot(module, key, nodes)

Map.put(acc, hash_slot, [entry | Map.get(acc, hash_slot, [])])

key, acc ->
hash_slot = hash_slot(module, key, nodes)
@spec group_keys_by_hash_slot(Enum.t(), nodes_config, module, atom()) :: map
def group_keys_by_hash_slot(enum, nodes, module, :keys) do
Enum.group_by(enum, &hash_slot(module, &1, nodes))
end

Map.put(acc, hash_slot, [key | Map.get(acc, hash_slot, [])])
end)
def group_keys_by_hash_slot(enum, nodes, module, :tuples) do
Enum.group_by(enum, fn {key, _} -> hash_slot(module, key, nodes) end)
end

## Private Functions
Expand Down
18 changes: 6 additions & 12 deletions lib/nebulex_redis_adapter/redis_cluster.ex
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,13 @@ defmodule NebulexRedisAdapter.RedisCluster do
end)
end

@spec group_keys_by_hash_slot(Enum.t(), module) :: map
def group_keys_by_hash_slot(enum, keyslot) do
Enum.reduce(enum, %{}, fn
{key, _} = entry, acc ->
slot = hash_slot(key, keyslot)

Map.put(acc, slot, [entry | Map.get(acc, slot, [])])

key, acc ->
slot = hash_slot(key, keyslot)
@spec group_keys_by_hash_slot(Enum.t(), module, atom()) :: map
def group_keys_by_hash_slot(enum, keyslot, :keys) do
Enum.group_by(enum, &hash_slot(&1, keyslot))
end

Map.put(acc, slot, [key | Map.get(acc, slot, [])])
end)
def group_keys_by_hash_slot(enum, keyslot, :tuples) do
Enum.group_by(enum, fn {key, _} -> hash_slot(key, keyslot) end)
end

@spec hash_slot(any, module) :: {:"$hash_slot", pos_integer}
Expand Down
25 changes: 25 additions & 0 deletions test/nebulex_redis_adapter/redis_cluster_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,31 @@ defmodule NebulexRedisAdapter.RedisClusterTest do

assert Cache.get_all(["{foo}.1", "{foo}.2"]) == %{"{foo}.1" => "bar1", "{foo}.2" => "bar2"}
end

test "put and get operations with tupled keys" do
assert Cache.put_all(%{
{RedisCache.Testing, "key1"} => "bar1",
{RedisCache.Testing, "key2"} => "bar2"
}) == :ok

assert Cache.get_all([{RedisCache.Testing, "key1"}, {RedisCache.Testing, "key2"}]) == %{
{RedisCache.Testing, "key1"} => "bar1",
{RedisCache.Testing, "key2"} => "bar2"
}

assert Cache.put_all(%{
{RedisCache.Testing, {Nested, "key1"}} => "bar1",
{RedisCache.Testing, {Nested, "key2"}} => "bar2"
}) == :ok

assert Cache.get_all([
{RedisCache.Testing, {Nested, "key1"}},
{RedisCache.Testing, {Nested, "key2"}}
]) == %{
{RedisCache.Testing, {Nested, "key1"}} => "bar1",
{RedisCache.Testing, {Nested, "key2"}} => "bar2"
}
end
end

describe "MOVED" do
Expand Down