Skip to content

Commit

Permalink
Add :dynamic_cache to set the cache instance dynamically in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
cabol committed Aug 29, 2022
1 parent f72d4d7 commit 88aa021
Show file tree
Hide file tree
Showing 24 changed files with 298 additions and 204 deletions.
13 changes: 12 additions & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ locals_without_parens = [
unwrap_or_raise: 1,
wrap_ok: 1,
wrap_error: 1,
wrap_error: 2
wrap_error: 2,

# Nebulex.Cache
dynamic_cache: 2,

# Tests
deftests: 1,
deftests: 2,
setup_with_cache: 1,
setup_with_cache: 2,
setup_with_dynamic_cache: 2,
setup_with_dynamic_cache: 3
]

[
Expand Down
8 changes: 4 additions & 4 deletions lib/nebulex/adapter/entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ defmodule Nebulex.Adapter.Entry do
See `c:Nebulex.Cache.exists?/1`.
"""
@callback exists?(adapter_meta, key) :: Nebulex.Cache.ok_error_tuple(boolean)
@callback exists?(adapter_meta, key, opts) :: Nebulex.Cache.ok_error_tuple(boolean)

@doc """
Returns the remaining time-to-live for the given `key`.
Expand All @@ -188,7 +188,7 @@ defmodule Nebulex.Adapter.Entry do
See `c:Nebulex.Cache.ttl/1`.
"""
@callback ttl(adapter_meta, key) ::
@callback ttl(adapter_meta, key, opts) ::
Nebulex.Cache.ok_error_tuple(value, Nebulex.Cache.fetch_error_reason())

@doc """
Expand All @@ -199,7 +199,7 @@ defmodule Nebulex.Adapter.Entry do
See `c:Nebulex.Cache.expire/2`.
"""
@callback expire(adapter_meta, key, ttl) :: Nebulex.Cache.ok_error_tuple(boolean)
@callback expire(adapter_meta, key, ttl, opts) :: Nebulex.Cache.ok_error_tuple(boolean)

@doc """
Returns `{:ok, true}` if the given `key` exists and the last access time was
Expand All @@ -209,5 +209,5 @@ defmodule Nebulex.Adapter.Entry do
See `c:Nebulex.Cache.touch/1`.
"""
@callback touch(adapter_meta, key) :: Nebulex.Cache.ok_error_tuple(boolean)
@callback touch(adapter_meta, key, opts) :: Nebulex.Cache.ok_error_tuple(boolean)
end
8 changes: 4 additions & 4 deletions lib/nebulex/adapters/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,15 @@ defmodule Nebulex.Adapters.Local do
end

@impl true
def exists?(adapter_meta, key) do
def exists?(adapter_meta, key, _opts) do
case fetch(adapter_meta, key, []) do
{:ok, _} -> {:ok, true}
{:error, _} -> {:ok, false}
end
end

@impl true
defspan ttl(adapter_meta, key) do
defspan ttl(adapter_meta, key, _opts) do
with {:ok, res} <- adapter_meta.meta_tab |> list_gen() |> do_fetch(key, adapter_meta) do
{:ok, entry_ttl(res)}
end
Expand All @@ -571,7 +571,7 @@ defmodule Nebulex.Adapters.Local do
end

@impl true
defspan expire(adapter_meta, key, ttl) do
defspan expire(adapter_meta, key, ttl, _opts) do
now = Time.now()

adapter_meta.meta_tab
Expand All @@ -580,7 +580,7 @@ defmodule Nebulex.Adapters.Local do
end

@impl true
defspan touch(adapter_meta, key) do
defspan touch(adapter_meta, key, _opts) do
adapter_meta.meta_tab
|> update_entry(adapter_meta.backend, key, [{4, Time.now()}])
|> wrap_ok()
Expand Down
16 changes: 8 additions & 8 deletions lib/nebulex/adapters/multilevel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ defmodule Nebulex.Adapters.Multilevel do
end

@impl true
defspan exists?(adapter_meta, key) do
defspan exists?(adapter_meta, key, opts) do
Enum.reduce_while(adapter_meta.levels, {:ok, false}, fn l_meta, acc ->
case with_dynamic_cache(l_meta, :exists?, [key]) do
case with_dynamic_cache(l_meta, :exists?, [key, opts]) do
{:ok, true} -> {:halt, {:ok, true}}
{:ok, false} -> {:cont, acc}
{:error, _} = error -> {:halt, error}
Expand All @@ -437,11 +437,11 @@ defmodule Nebulex.Adapters.Multilevel do
end

@impl true
defspan ttl(adapter_meta, key) do
defspan ttl(adapter_meta, key, opts) do
default = wrap_error Nebulex.KeyError, key: key, cache: adapter_meta.name

Enum.reduce_while(adapter_meta.levels, default, fn l_meta, acc ->
case with_dynamic_cache(l_meta, :ttl, [key]) do
case with_dynamic_cache(l_meta, :ttl, [key, opts]) do
{:ok, _} = ok -> {:halt, ok}
{:error, %Nebulex.KeyError{}} -> {:cont, acc}
{:error, _} = error -> {:halt, error}
Expand All @@ -450,13 +450,13 @@ defmodule Nebulex.Adapters.Multilevel do
end

@impl true
defspan expire(adapter_meta, key, ttl) do
eval_while(adapter_meta, :expire, [key, ttl], {:ok, false}, &(&1 or &2))
defspan expire(adapter_meta, key, ttl, opts) do
eval_while(adapter_meta, :expire, [key, ttl, opts], {:ok, false}, &(&1 or &2))
end

@impl true
defspan touch(adapter_meta, key) do
eval_while(adapter_meta, :touch, [key], {:ok, false}, &(&1 or &2))
defspan touch(adapter_meta, key, opts) do
eval_while(adapter_meta, :touch, [key, opts], {:ok, false}, &(&1 or &2))
end

## Nebulex.Adapter.Queryable
Expand Down
8 changes: 4 additions & 4 deletions lib/nebulex/adapters/nil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,18 @@ defmodule Nebulex.Adapters.Nil do
end

@impl true
def exists?(_, _), do: {:ok, false}
def exists?(_, _, _), do: {:ok, false}

@impl true
def ttl(adapter_meta, key) do
def ttl(adapter_meta, key, _opts) do
wrap_error Nebulex.KeyError, key: key, cache: adapter_meta.cache
end

@impl true
def expire(_, _, _), do: {:ok, false}
def expire(_, _, _, _), do: {:ok, false}

@impl true
def touch(_, _), do: {:ok, false}
def touch(_, _, _), do: {:ok, false}

@impl true
def update_counter(_, _, amount, _, default, _) do
Expand Down
16 changes: 8 additions & 8 deletions lib/nebulex/adapters/partitioned.ex
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ defmodule Nebulex.Adapters.Partitioned do
end

@impl true
defspan exists?(adapter_meta, key) do
call(adapter_meta, key, :exists?, [key])
defspan exists?(adapter_meta, key, opts) do
call(adapter_meta, key, :exists?, [key, opts])
end

@impl true
Expand All @@ -510,20 +510,20 @@ defmodule Nebulex.Adapters.Partitioned do
end

@impl true
defspan ttl(adapter_meta, key) do
defspan ttl(adapter_meta, key, opts) do
adapter_meta
|> call(key, :ttl, [key])
|> call(key, :ttl, [key, opts])
|> handle_key_error(adapter_meta.name)
end

@impl true
defspan expire(adapter_meta, key, ttl) do
call(adapter_meta, key, :expire, [key, ttl])
defspan expire(adapter_meta, key, ttl, opts) do
call(adapter_meta, key, :expire, [key, ttl, opts])
end

@impl true
defspan touch(adapter_meta, key) do
call(adapter_meta, key, :touch, [key])
defspan touch(adapter_meta, key, opts) do
call(adapter_meta, key, :touch, [key, opts])
end

## Nebulex.Adapter.Queryable
Expand Down
16 changes: 8 additions & 8 deletions lib/nebulex/adapters/replicated.ex
Original file line number Diff line number Diff line change
Expand Up @@ -418,25 +418,25 @@ defmodule Nebulex.Adapters.Replicated do
end

@impl true
defspan exists?(adapter_meta, key) do
with_dynamic_cache(adapter_meta, :exists?, [key])
defspan exists?(adapter_meta, key, opts) do
with_dynamic_cache(adapter_meta, :exists?, [key, opts])
end

@impl true
defspan ttl(adapter_meta, key) do
defspan ttl(adapter_meta, key, opts) do
adapter_meta
|> with_dynamic_cache(:ttl, [key])
|> with_dynamic_cache(:ttl, [key, opts])
|> handle_key_error(adapter_meta.name)
end

@impl true
defspan expire(adapter_meta, key, ttl) do
with_transaction(adapter_meta, :expire, [key], [key, ttl])
defspan expire(adapter_meta, key, ttl, opts) do
with_transaction(adapter_meta, :expire, [key], [key, ttl, opts])
end

@impl true
defspan touch(adapter_meta, key) do
with_transaction(adapter_meta, :touch, [key], [key])
defspan touch(adapter_meta, key, opts) do
with_transaction(adapter_meta, :touch, [key], [key, opts])
end

## Nebulex.Adapter.Queryable
Expand Down
Loading

0 comments on commit 88aa021

Please sign in to comment.