Memcached comma...login
Memcached command operation manual
author:php.cn  update time:2022-04-13 17:53:40

Memcached CAS command


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

It is only the last time on the current client After the value is obtained, the value can be written only if 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 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 parameter description is as follows:

  • ##key: The key in the key-value structure is used to find cached values.

  • flags: Integer parameter that can include key-value pairs and is used by the client to store additional information about the key-value pairs .

  • exptime: The length of time to keep key-value pairs in the cache (in seconds, 0 means forever)

  • bytes: The number of bytes stored in the cache

  • unique_cas_tokenA unique 64-bit value obtained through the gets command.

  • noreply (optional) : This parameter tells the server that no data is required to be returned

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

Example

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

The gets command functions like 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 for a name/value pair.

The example steps are as follows:

  • If the unique token is not set, the CAS command execution error occurs.

  • If the key key does not exist, the execution fails.

  • Add key-value pairs.

  • Get the unique token through gets command.

  • Use the cas command to update the data

  • Use the get command to check whether the data is updated

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

STORED

Output information description:

  • STORED: Output after successful saving.

  • ERROR: Saving 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.