Latest web development tutorials

Memcached CAS command

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

It is only after the current client last value, the key value corresponding to the case where no other client modified to be able to write values.

Checks are carried out by cas_token parameters, this parameter is a unique 64-bit value Memcach assigned to the existing elements.

grammar:

The basic syntax CAS command is as follows:

cas key flags exptime bytes unique_cas_token [noreply]
value

Parameters are as follows:

  • key: key key-value structure key, it is used to find the cache value.
  • flags: may include a key-value pair of integer parameter, the client uses it to store additional information about the key-value pairs.
  • exptime: save key-value pairs in the cache the length of time (in seconds, 0 means forever)
  • bytes: number of bytes stored in the cache
  • unique_cas_token acquired through the command gets a unique 64-bit value.
  • noreply (optional): This parameter tells the server does not need to return data
  • value: the value stored (always located in the second row) (can be directly understood as key-value structure value)

Examples

To use CAS commands on Memcached, you need to get a token (token) from Memcached service providers through gets command.

Function gets command similar basic get command. The difference between the two commands is that, gets a little more information returned: 64-bit integer values ​​very much like the name / value pairs "version" identifier.

Examples of the following steps:

  • If you do not set a unique token, the CAS command execution error.
  • If the key key does not exist, execution failed.
  • Add key-value pairs.
  • Get unique token command gets through.
  • Using the cas command to update data
  • Whether to use the get command to view the data update
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

Export

If the data is added successfully, the output:

STORED

Output information Description:

  • STORED: After successfully saved output.
  • ERROR: save error or a syntax error.
  • EXISTS: after the last value of the other users are updating the data.
  • NOT_FOUND: This key does not exist on Memcached services.