Memcache.cas
cas
, go back to Memcache module for more information.
Specs
Compare and swap value using optimistic locking.
"Happy path":
- Get the existing value for key
- If it exists, call the update function with the value
- Set the returned value for key
The update can be retried in case an intervening update happened.
A default value can also be provided, to be used if the key does not yet exist.
See the description of options below for details.
Options
:retry
- whether the update should be retried if someone else has updated the value for the same key in the meantime. Defaults totrue
.:ttl
- TTL for the updated key. Defaults to the TTL passed when starting the server viastart_link/2
, or infinity if none was given there either.:default
- a default value to use if the key does not yet exist. If a 0-arity function is passed for this value, the default value will be lazily evaluated only when needed. This is useful in cases where the default is expensive to compute. If a:default
opt is not passed, the function will return{:error, "Key not found"}
when attempting to update a nonexistent key.
NOTE: If a key is created after the initial GET and before the default value is ADDed, the update will be retried at least once regardless of the value of the:retry
opt.