YouTip LogoYouTip

Memcached Cas

# Memcached CAS Command Memcached CAS (Check-And-Set or Compare-And-Swap) command is used to perform a "check and set" operation. It will only write the value if the value corresponding to the key has not been modified by another client since the current client last retrieved it. The check is performed via the `unique_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 value The parameters are explained as follows: * **key:** The key in the key-value structure, used to look up the cached value. * **flags:** An integer parameter that can include key-value pairs. The client uses it to store additional information about the key-value pair. * **exptime:** The length of time (in seconds) to keep the key-value pair in the cache (0 means forever). * **bytes:** The number of bytes to store in the cache. * **unique_cas_token:** A unique 64-bit value obtained via the `gets` command. * **noreply (optional):** This parameter tells the server not to return data. * **value:** The value to store (always on the second line) (can be understood as the value in the key-value structure). ### Example To use the CAS command on Memcached, you need to obtain a token from the Memcached server using the `gets` command. The `gets` command functions similarly to the basic `get` command. The difference between the two commands is that `gets` returns slightly more information: a 64-bit integer value that acts like a "version" identifier for the name/value pair. The example steps are as follows: * If no unique token is set, the CAS command execution will error. * If the key does not exist, execution fails. * Add a key-value pair. * Obtain the unique token via the `gets` command. * Use the `cas` command to update the data. * Use the `get` command to check if the data has been updated. cas tp 0 900 9 ERROR <βˆ’ Missing 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 is: STORED Output message explanation: * **STORED:** Output after successful save. * **ERROR:** Save error or syntax error. * **EXISTS:** Another user has updated the data since the last retrieval. * **NOT_FOUND:** The key does not exist on the Memcached service.
← Ionic RangeIonic Checkbox β†’