Memcached CAS command


Release date:2024-03-18 Update date:2024-03-19 Editor:admin View counts:208

Label:

Memcached CAS command

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

It is only after the last value of the current client, the key The value can only be written if the corresponding value has not been modified by other clients.

The check is passed. cas_token Parameter, which is a unique 64-bit value assigned by Memcach to an element that already exists.

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:

  • key Key value key-value In the structure key Used to find cached values

  • flags Can include integer parameters for key-value pairs, which clients use to store additional information about key-value pairs

  • exptime Length of time to save key-value pairs in the cache (in seconds, 0 means forever)

  • bytes Number of bytes stored in the cache

  • unique_cas_token Pass through gets A unique 64-bit value obtained by the command.

  • noreply (optional): this parameter tells the server that there is no need to return data

  • value The stored value (always in the second line) (which can be directly understood as key-value In the structure value )

Example

To use the CAS command on Memcached, you need to get through the Memcached service provider gets Command to get the token (token).

gets The function of the command is similar to the basic get Orders. The difference between the two commands is that gets A little more information is returned: the 64-bit integer value is very much like the “version” identifier of the name / value pair.

The steps of the example are as follows:

  • If no unique token is set, the CAS command executes incorrectly.

  • If the key key Does not exist, execution failed.

  • Add key-value pairs.

  • Pass through gets Command to get a unique token.

  • Use cas Command to update data

  • Use get Command to see if the data is updated

cas tp 0 900 9
ERROR             <− lack token

cas tp 0 900 9 2
memcached
NOT_FOUND         <− Key tp does not exist

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, the output:

STORED

Output information description:

  • STORED Output after being saved successfully

  • ERROR Save error or syntax error.

  • EXISTS Another user is also updating the data after the last value

  • NOT_FOUND The key value does not exist on the Memcached service

Powered by TorCMS (https://github.com/bukun/TorCMS).