memcachex v0.1.0 Memcache
This module provides a user friendly API to interact with the
memcached server. All the functions delegate to
Memcache.Connection
. The pid
obtained using start_link/2
or
Memcache.Connection.start_link/2
can be used interchangeably with
both modules.
CAS
CAS feature allows to atomically perform two commands on a key. Get the cas version number associated with a key during the first command and pass that value during the second command. The second command will fail if the value has changed by someone else in the mean time.
{:ok, "hello", cas} = Memcache.get(pid, "key", cas: true)
{:ok} = Memcache.set_cas(pid, "key", "world", cas)
Memcache module provides a *_cas variant for most of the
functions. This function will take an additional argument named
cas
and returns the same value as their counterpart except in case
of CAS error. In case of CAS error the returned value would be equal
to { :error, "Key exists" }
Options
Most the functions in this module accepts an optional Keyword
list. The below list specifies the behavior of each option. The list
of option accepted by a specific function will be documented in the
specific funcion.
:cas
- (boolean) returns the CAS value associated with the data. This value will be either in second or third position of the returned tuple depending on the command. Defaults tofalse
.:ttl
- (integer) specifies the expiration time in seconds for the corresponding key. Can be set to0
to disable expiration. Defaults to0
.
Summary
Functions
Sets the key to value if the key doesn’t exist already. Returns
{:error, "Key exists"}
if the given key already exists
Appends the value to the end of the current value of the
key. Returns {:error, "Item not stored"}
if the item is not present
in the server already
Appends the value to the end of the current value of the key if the CAS value is equal to the provided value
Compare and swap value using optimistic locking
Decremens the current value. Only integer value can be
decremented. Returns { :error, "Incr/Decr on non-numeric value"}
if
the value stored in the server is not numeric
Decrements the current value if the CAS value is equal to the provided value
Removes the item with the given key value. Returns { :error, "Key
not found" }
if the given key is not found
Removes the item with the given key value if the CAS value is equal to the provided value
See Memcache.Connection.execute/3
Flush all the items in the server. ttl
option will cause the flush
to be delayed by the specified time
Gets the value associated with the key. Returns {:error, "Key not
found"}
if the given key doesn’t exist
Increments the current value. Only integer value can be
incremented. Returns { :error, "Incr/Decr on non-numeric value"}
if
the value stored in the server is not numeric
Increments the current value if the CAS value is equal to the provided value
Sends a noop command
Prepends the value to the start of the current value of the
key. Returns {:error, "Item not stored"}
if the item is not present
in the server already
Prepends the value to the start of the current value of the key if the CAS value is equal to the provided value
Sets the key to value if the key already exists. Returns {:error,
"Key not found"}
if the given key doesn’t exist
Sets the key to value if the key already exists and has CAS value equal to the provided value
Sets the key to value
Sets the key to value if the key exists and has CAS value equal to the provided value
See Memcache.Connection.start_link/0
See Memcache.Connection.start_link/1
Gets the default set of server statistics
Gets the specific set of server statistics
Gets the version of the server
Types
error :: {:error, binary | atom}
fetch_integer_result ::
{:ok, integer} |
{:ok, integer, integer} |
error
fetch_result ::
{:ok, binary} |
{:ok, binary, integer} |
error
store_result :: {:ok} | {:ok, integer} | error
Functions
Specs
add(GenServer.server, binary, binary, Keyword.t) :: store_result
Sets the key to value if the key doesn’t exist already. Returns
{:error, "Key exists"}
if the given key already exists.
Accepted options: :cas
, :ttl
Specs
append(GenServer.server, binary, binary, Keyword.t) :: store_result
Appends the value to the end of the current value of the
key. Returns {:error, "Item not stored"}
if the item is not present
in the server already
Accepted options: :cas
Specs
append_cas(GenServer.server, binary, binary, integer, Keyword.t) :: store_result
Appends the value to the end of the current value of the key if the CAS value is equal to the provided value
Accepted options: :cas
Specs
cas(GenServer.server, binary, (binary -> binary), Keyword.t) :: fetch_result
Compare and swap value using optimistic locking.
- Get the existing value for key
- If it exists, call the update function with the value
- Set the returned value for key
The 3rd operation will fail if someone else has updated the value
for the same key in the mean time. In that case, by default, this
function will go to step 1 and try again. Retry behavior can be
disabled by passing [retry: false]
option.
Specs
decr(GenServer.server, binary, Keyword.t) :: fetch_integer_result
Decremens the current value. Only integer value can be
decremented. Returns { :error, "Incr/Decr on non-numeric value"}
if
the value stored in the server is not numeric.
Options
:by
- (integer) The amount to add to the existing value. Defaults to1
.:default
- (integer) Default value to use in case the key is not found. Defaults to0
.
other options: :cas
, :ttl
Specs
decr_cas(GenServer.server, binary, integer, Keyword.t) :: fetch_integer_result
Decrements the current value if the CAS value is equal to the provided value.
Options
:by
- (integer) The amount to add to the existing value. Defaults to1
.:default
- (integer) Default value to use in case the key is not found. Defaults to0
.
other options: :cas
, :ttl
Specs
delete(GenServer.server, binary) :: store_result
Removes the item with the given key value. Returns { :error, "Key
not found" }
if the given key is not found
Specs
delete_cas(GenServer.server, binary, integer) :: store_result
Removes the item with the given key value if the CAS value is equal to the provided value
Specs
flush(GenServer.server, Keyword.t) :: store_result
Flush all the items in the server. ttl
option will cause the flush
to be delayed by the specified time.
Accepted options: :ttl
Specs
get(GenServer.server, binary, Keyword.t) :: fetch_result
Gets the value associated with the key. Returns {:error, "Key not
found"}
if the given key doesn’t exist.
Accepted option: :cas
Specs
incr(GenServer.server, binary, Keyword.t) :: fetch_integer_result
Increments the current value. Only integer value can be
incremented. Returns { :error, "Incr/Decr on non-numeric value"}
if
the value stored in the server is not numeric.
Options
:by
- (integer) The amount to add to the existing value. Defaults to1
.:default
- (integer) Default value to use in case the key is not found. Defaults to0
.
other options: :cas
, :ttl
Specs
incr_cas(GenServer.server, binary, integer, Keyword.t) :: fetch_integer_result
Increments the current value if the CAS value is equal to the provided value.
Options
:by
- (integer) The amount to add to the existing value. Defaults to1
.:default
- (integer) Default value to use in case the key is not found. Defaults to0
.
other options: :cas
, :ttl
Specs
prepend(GenServer.server, binary, binary, Keyword.t) :: store_result
Prepends the value to the start of the current value of the
key. Returns {:error, "Item not stored"}
if the item is not present
in the server already
Accepted options: :cas
Specs
prepend_cas(GenServer.server, binary, binary, integer, Keyword.t) :: store_result
Prepends the value to the start of the current value of the key if the CAS value is equal to the provided value
Accepted options: :cas
Specs
replace(GenServer.server, binary, binary, Keyword.t) :: store_result
Sets the key to value if the key already exists. Returns {:error,
"Key not found"}
if the given key doesn’t exist.
Accepted options: :cas
, :ttl
Specs
replace_cas(GenServer.server, binary, binary, integer, Keyword.t) :: store_result
Sets the key to value if the key already exists and has CAS value equal to the provided value.
Accepted options: :cas
, :ttl
Specs
set(GenServer.server, binary, binary, Keyword.t) :: store_result
Sets the key to value
Accepted options: :cas
, :ttl
Specs
set_cas(GenServer.server, binary, binary, integer, Keyword.t) :: store_result
Sets the key to value if the key exists and has CAS value equal to the provided value
Accepted options: :cas
, :ttl
Specs
stat(GenServer.server) :: HashDict.t | error
Gets the default set of server statistics
Specs
stat(GenServer.server, String.t) ::
HashDict.t |
error
Gets the specific set of server statistics
Specs
version(GenServer.server) :: String.t | error
Gets the version of the server