Skip to content

Commit

Permalink
[#189] All commands optionally support a dynamic cache as the first a…
Browse files Browse the repository at this point in the history
…rgument
  • Loading branch information
cabol committed Mar 31, 2023
1 parent 0fb0452 commit a6ef459
Show file tree
Hide file tree
Showing 23 changed files with 916 additions and 443 deletions.
6 changes: 3 additions & 3 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
locals_without_parens = [
# Nebulex.Helpers
# Nebulex.Utils
unwrap_or_raise: 1,
wrap_ok: 1,
wrap_error: 1,
wrap_error: 2,

# Nebulex.Cache
dynamic_cache: 2,
# Nebulex.Cache.Utils
defcacheapi: 2,

# Nebulex.Caching
cache_ref: 1,
Expand Down
4 changes: 2 additions & 2 deletions guides/creating-new-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ defmodule NebulexMemoryAdapter do
@behaviour Nebulex.Adapter
@behaviour Nebulex.Adapter.Queryable

import Nebulex.Helpers
import Nebulex.Utils

@impl Nebulex.Adapter
defmacro __before_compile__(_env), do: :ok
Expand Down Expand Up @@ -308,7 +308,7 @@ defmodule NebulexMemoryAdapter do
@behaviour Nebulex.Adapter.KV
@behaviour Nebulex.Adapter.Queryable

import Nebulex.Helpers
import Nebulex.Utils

@impl Nebulex.Adapter
defmacro __before_compile__(_env), do: :ok
Expand Down
2 changes: 1 addition & 1 deletion lib/nebulex/adapter/persistence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Nebulex.Adapter.Persistence do
quote do
@behaviour Nebulex.Adapter.Persistence

import Nebulex.Helpers
import Nebulex.Utils, only: [wrap_error: 2]

@impl true
def dump(%{cache: cache}, path, opts) do
Expand Down
2 changes: 1 addition & 1 deletion lib/nebulex/adapter/stats.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Nebulex.Adapter.Stats do
@behaviour Nebulex.Adapter.Stats

import Nebulex.Adapter.Stats, only: [defspan: 2, defspan: 3]
import Nebulex.Helpers
import Nebulex.Utils, only: [wrap_error: 2]

@impl true
def stats(adapter_meta, _opts) do
Expand Down
51 changes: 26 additions & 25 deletions lib/nebulex/adapter/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,40 @@ defmodule Nebulex.Adapter.Transaction do
Locking only the involved key (recommended):
MyCache.transaction [keys: [:counter]], fn ->
counter = MyCache.get(:counter)
MyCache.set(:counter, counter + 1)
end
MyCache.transaction [keys: [:alice, :bob]], fn ->
alice = MyCache.get(:alice)
bob = MyCache.get(:bob)
MyCache.set(:alice, %{alice | balance: alice.balance + 100})
MyCache.set(:bob, %{bob | balance: bob.balance + 100})
end
MyCache.transaction(
fn ->
counter = MyCache.get(:counter)
MyCache.set(:counter, counter + 1)
end,
[keys: [:counter]]
)
MyCache.transaction(
fn ->
alice = MyCache.get(:alice)
bob = MyCache.get(:bob)
MyCache.set(:alice, %{alice | balance: alice.balance + 100})
MyCache.set(:bob, %{bob | balance: bob.balance + 100})
end,
[keys: [:alice, :bob]]
)
"""

@doc """
Runs the given function inside a transaction.
A successful transaction returns the value returned by the function wrapped
in a tuple as `{:ok, value}`.
In case the transaction cannot be executed, then `{:error, reason}` is
returned.
If an Elixir exception occurs, the exception will bubble up from the
transaction function. If the transaction is aborted,
`{:error, Nebulex.Error.t()}` is returned.
If an unhandled error/exception occurs, the error will bubble up from the
transaction function.
If `transaction/2` is called inside another transaction, the function is
simply executed without wrapping the new transaction call in any way.
A successful transaction returns the value returned by the function wrapped
in a tuple as `{:ok, any()}`.
See `c:Nebulex.Cache.transaction/2`.
"""
@callback transaction(Nebulex.Adapter.adapter_meta(), Nebulex.Cache.opts(), (() -> any())) ::
any()
@callback transaction(Nebulex.Adapter.adapter_meta(), fun(), Nebulex.Cache.opts()) ::
Nebulex.Cache.ok_error_tuple(any())

@doc """
Returns `{:ok, true}` if the current process is inside a transaction,
Expand All @@ -85,10 +86,10 @@ defmodule Nebulex.Adapter.Transaction do
quote do
@behaviour Nebulex.Adapter.Transaction

import Nebulex.Helpers
import Nebulex.Utils, only: [wrap_ok: 1, wrap_error: 2]

@impl true
def transaction(%{cache: cache, pid: pid} = adapter_meta, opts, fun) do
def transaction(%{cache: cache, pid: pid} = adapter_meta, fun, opts) do
adapter_meta
|> do_in_transaction?()
|> do_transaction(
Expand Down
2 changes: 1 addition & 1 deletion lib/nebulex/adapters/nil.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ defmodule Nebulex.Adapters.Nil do
# Inherit default transaction implementation
use Nebulex.Adapter.Transaction

import Nebulex.Helpers
import Nebulex.Utils, only: [wrap_error: 2]

## Nebulex.Adapter

Expand Down
Loading

0 comments on commit a6ef459

Please sign in to comment.