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 support for startup matching documentation #384

Merged
merged 2 commits into from
Oct 14, 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
8 changes: 7 additions & 1 deletion lib/cachex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ defmodule Cachex do
#
# Without this, Cachex is not compatible per Elixir's documentation.
@spec start_link(atom | Keyword.t()) :: {atom, pid} | {:error, :invalid_name}
def start_link([name]) when is_atom(name),
do: start_link(name)

def start_link([name, options]) when is_atom(name) and is_list(options),
do: start_link(name, options)

def start_link(options) when is_list(options) do
case Keyword.fetch(options, :name) do
{:ok, name} ->
Expand All @@ -297,7 +303,7 @@ defmodule Cachex do
end

def start_link(name) when is_atom(name),
do: start_link(name: name)
do: start_link(name, [])

@doc """
Creates a new Cachex cache service tree.
Expand Down
28 changes: 23 additions & 5 deletions test/cachex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,30 @@ defmodule CachexTest do
# This test validates `Cachex.start_link/1` mtaintains compatibility
# with `Supervisor.child_spec/2` and handles the name as an option.
test "cache start with child_spec/1 compatibility" do
# create two caches using `Cachex.start_link/1`
# check the default ways of starting a cache
{:ok, _pid} = Cachex.start_link(:child_spec1)
{:ok, _pid} = Cachex.start_link(name: :child_spec2)
{:ok, _pid} = Cachex.start_link(:child_spec2, transactions: true)

# verify that both are at least alive in some way
{:ok, _cache1} = Cachex.inspect(:child_spec1, :cache)
{:ok, _cache2} = Cachex.inspect(:child_spec2, :cache)
# check the format {Cachex, [:child_spec3]}
{:ok, _pid} = Cachex.start_link([:child_spec3])

# check the format {Cachex, [:child_spec4, []]}
{:ok, _pid} = Cachex.start_link([:child_spec4, [transactions: true]])

# check the format {Cachex, [name: :child_spec5]}
{:ok, _pid} = Cachex.start_link(name: :child_spec5)

# check the format {Cachex, [name: :child_spec6, transactions: true]}
{:ok, _pid} = Cachex.start_link(name: :child_spec6, transactions: true)

# verify the caches that are created only from a name
{:ok, cache()} = Cachex.inspect(:child_spec1, :cache)
{:ok, cache()} = Cachex.inspect(:child_spec3, :cache)
{:ok, cache()} = Cachex.inspect(:child_spec5, :cache)

# double check those with options provided by double checking the value
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec2, :cache)
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec4, :cache)
{:ok, cache(transactions: true)} = Cachex.inspect(:child_spec6, :cache)
end
end
Loading