View Source Nebulex.Adapter.Transaction behaviour (Nebulex v2.6.4)
Specifies the adapter transactions API.
Default implementation
This module also provides a default implementation which uses the Erlang
library :global
.
This implementation accepts the following options:
:keys
- The list of the keys that will be locked. Since the lock id is generated based on the key, if this option is not set, a fixed/constant lock id is used to perform the transaction, then all further transactions (without this option set) are serialized and the performance is affected significantly. For that reason it is recommended to pass the list of keys involved in the transaction.:nodes
- The list of the nodes where the lock will be set, or on all nodes if none are specified.:retries
- If the key has been locked by other process already, and:retries
is not equal to 0, the process sleeps for a while and tries to execute the action later. When:retries
attempts have been made, an exception is raised. If:retries
is:infinity
(the default), the function will eventually be executed (unless the lock is never released).
Let's see an example:
MyCache.transaction fn ->
counter = MyCache.get(:counter)
MyCache.set(:counter, counter + 1)
end
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
Summary
Callbacks
Returns true
if the given process is inside a transaction.
Runs the given function inside a transaction.
Callbacks
@callback in_transaction?(Nebulex.Adapter.adapter_meta()) :: boolean()
Returns true
if the given process is inside a transaction.
@callback transaction(Nebulex.Adapter.adapter_meta(), Nebulex.Cache.opts(), (... -> any())) :: any()
Runs the given function inside a transaction.
A successful transaction returns the value returned by the function.