Easy Tutorial
❮ Memcached Prepend Data Memcached Replace Data ❯

Memcached CAS Command

The Memcached CAS (Check-And-Set or Compare-And-Swap) command is used to perform a "check and set" operation. It only allows the value to be written if the value associated with the key has not been modified by another client since the last time the current client retrieved it.

The check is performed using the cas_token parameter, which is a unique 64-bit value assigned by Memcached to an existing element.

Syntax:

The basic syntax format for the CAS command is as follows:

cas key flags exptime bytes unique_cas_token [noreply]
value

Parameter descriptions are as follows:

Example

To use the CAS command on Memcached, you need to obtain a token from the Memcached service provider via the gets command.

The gets command is similar to the basic get command, but it returns slightly more information: a 64-bit integer value that acts as a "version" identifier for the name/value pair.

Example steps are as follows:

Output

If the data is added successfully, the output will be:

STORED

Output information descriptions are as follows:

❮ Memcached Prepend Data Memcached Replace Data ❯