Skip to content
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
28 changes: 19 additions & 9 deletions lib/cache/counter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,25 @@ defmodule Cache.Counter do

@impl Cache
def start_link(opts) do
Task.start_link(fn ->
cache_name = opts[:table_name]
initial_size = opts[:initial_size] || 1
counters_opts = if opts[:write_concurrency], do: [:atomics, :write_concurrency], else: [:atomics]
ref = :counters.new(initial_size, counters_opts)
:persistent_term.put({cache_name, @ref_key}, ref)
:persistent_term.put({cache_name, @size_key}, initial_size)
Process.hibernate(Function, :identity, [nil])
end)
parent = self()
ready_ref = make_ref()

{:ok, pid} =
Task.start_link(fn ->
cache_name = opts[:table_name]
initial_size = opts[:initial_size] || 1
counters_opts = if opts[:write_concurrency], do: [:atomics, :write_concurrency], else: [:atomics]
ref = :counters.new(initial_size, counters_opts)
:persistent_term.put({cache_name, @ref_key}, ref)
:persistent_term.put({cache_name, @size_key}, initial_size)
send(parent, {ready_ref, :ready})
Process.hibernate(Function, :identity, [nil])
end)

receive do
{^ready_ref, :ready} -> {:ok, pid}
{:EXIT, ^pid, reason} -> {:error, reason}
end
end

@impl Cache
Expand Down
48 changes: 29 additions & 19 deletions lib/cache/dets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -365,25 +365,35 @@ defmodule Cache.DETS do

@impl Cache
def start_link(opts) do
Task.start_link(fn ->
table_name = opts[:table_name]

file_path =
opts[:file_path]
|> to_string
|> create_file_name(table_name)
|> tap(&File.mkdir_p!(Path.dirname(&1)))
|> String.to_charlist()

opts =
opts
|> Keyword.drop([:table_name, :file_path])
|> Kernel.++(access: :read_write, file: file_path)

{:ok, _} = :dets.open_file(table_name, opts)

Process.hibernate(Function, :identity, [nil])
end)
parent = self()
ref = make_ref()

{:ok, pid} =
Task.start_link(fn ->
table_name = opts[:table_name]

file_path =
opts[:file_path]
|> to_string
|> create_file_name(table_name)
|> tap(&File.mkdir_p!(Path.dirname(&1)))
|> String.to_charlist()

opts =
opts
|> Keyword.drop([:table_name, :file_path])
|> Kernel.++(access: :read_write, file: file_path)

{:ok, _} = :dets.open_file(table_name, opts)

send(parent, {ref, :ready})
Process.hibernate(Function, :identity, [nil])
end)

receive do
{^ref, :ready} -> {:ok, pid}
{:EXIT, ^pid, reason} -> {:error, reason}
end
end

defp create_file_name(file_path, table_name) do
Expand Down
52 changes: 31 additions & 21 deletions lib/cache/ets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -617,30 +617,40 @@ defmodule Cache.ETS do

@impl Cache
def start_link(opts) do
Task.start_link(fn ->
table_name = opts[:table_name]
rehydration_path = opts[:rehydration_path]

ets_opts =
opts
|> Keyword.drop([:table_name, :type, :rehydration_path])
|> Kernel.++([opts[:type], :public, :named_table])

ets_opts =
if opts[:compressed] do
Keyword.delete(ets_opts, :compressed) ++ [:compressed]
else
ets_opts
parent = self()
ref = make_ref()

{:ok, pid} =
Task.start_link(fn ->
table_name = opts[:table_name]
rehydration_path = opts[:rehydration_path]

ets_opts =
opts
|> Keyword.drop([:table_name, :type, :rehydration_path])
|> Kernel.++([opts[:type], :public, :named_table])

ets_opts =
if opts[:compressed] do
Keyword.delete(ets_opts, :compressed) ++ [:compressed]
else
ets_opts
end

rehydrate_or_create_table(table_name, rehydration_path, ets_opts)

if rehydration_path do
setup_exit_signal_handlers(table_name, rehydration_path)
end

rehydrate_or_create_table(table_name, rehydration_path, ets_opts)
send(parent, {ref, :ready})
Process.hibernate(Function, :identity, [nil])
end)

if rehydration_path do
setup_exit_signal_handlers(table_name, rehydration_path)
end

Process.hibernate(Function, :identity, [nil])
end)
receive do
{^ref, :ready} -> {:ok, pid}
{:EXIT, ^pid, reason} -> {:error, reason}
end
end

defp rehydrate_or_create_table(table_name, rehydration_path, ets_opts)
Expand Down
Loading