Skip to content

Commit

Permalink
Add new cluster structures to cache records
Browse files Browse the repository at this point in the history
  • Loading branch information
whitfin committed Apr 1, 2024
1 parent 31c3fd0 commit a6efb17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/cachex/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,20 @@ defmodule Cachex.Options do
error(:invalid_nodes)

true ->
cache(cache, nodes: nodes)
cache(cache,
nodes: nodes,
cluster:
cluster(
enabled: nodes != [node()],
router: fn key, nodes ->
key
|> :erlang.phash2()
|> Jumper.slot(length(nodes))
end,
nodes: nodes
)
)

# coveralls-ignore-stop
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/cachex/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule Cachex.Spec do
@type cache ::
record(:cache,
name: atom,
cluster: cluster,
commands: map,
compressed: boolean,
expiration: expiration,
Expand All @@ -39,6 +40,14 @@ defmodule Cachex.Spec do
warmers: [warmer]
)

# Record specification for a cluster instance
@type cluster ::
record(:cluster,
enabled: boolean,
router: (any, any -> atom),
nodes: any
)

# Record specification for a command instance
@type command ::
record(:command,
Expand Down Expand Up @@ -119,6 +128,7 @@ defmodule Cachex.Spec do
"""
defrecord :cache,
name: nil,
cluster: nil,
commands: %{},
compressed: false,
expiration: nil,
Expand All @@ -130,6 +140,11 @@ defmodule Cachex.Spec do
transactions: false,
warmers: []

defrecord :cluster,
enabled: false,
router: nil,
nodes: nil

@doc """
Creates a command record from the provided values.
Expand Down Expand Up @@ -273,6 +288,12 @@ defmodule Cachex.Spec do
@spec cache(cache, Keyword.t()) :: cache
defmacro cache(record, args)

@doc """
Updates a cluster record from the provided values.
"""
@spec cluster(cluster, Keyword.t()) :: cluster
defmacro cluster(record, args)

@doc """
Updates a command record from the provided values.
"""
Expand Down

0 comments on commit a6efb17

Please sign in to comment.