Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Memcached CAS Command

Memcached CAS Command

The Memcached CAS (Check-And-Set or Compare-And-Swap) command is used to perform a "check and set" operation

It can only write the value if, after the current client last retrieved the value, the value corresponding to the key has not been modified by other clients.

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

Syntax:

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



cas key flags exptime bytes unique_cas_token [noreply]

value

The parameters are described 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. The difference between the two commands is that gets returns slightly more information: a 64-bit integer value that is very much like a "version" identifier of a name/value pair.

The example steps are as follows:



cas tp 0 900 9

ERROR             <− 缺少 token



cas tp 0 900 9 2

memcached

NOT_FOUND         <− 键 tp 不存在



set tp 0 900 9

memcached

STORED



gets tp

VALUE tp 0 9 1

memcached

END



cas tp 0 900 5 1

redis

STORED



get tp

VALUE tp 0 5

redis

END

Output

If the data is added successfully, it outputs:



STORED

Description of the output information: