Check and Set Operation aka CAS is a update for existing key-value. For example we get a key-value counter-123 and we increased it to locally 124. Then we try to update the key-value like counter-124 Memcached checks if the value is changed by others. If it is changed we get an error. To track changes Memcached gives us a token.
cas key flags expiretime bytes castoken
- cas the name of the operation
- key the key we want to change
- flags related flags
- expiretime key-value hold interval
- bytes the size of the value
- castoken the token used by Memcache to track change
Now lets make an example
As always like we do we add new key-value
add counter 0 0 3 123 STORED
Now we get the variable but this get operation is a bit different from get because we use gets like below which provides a token too.
gets counter VALUE counter 0 3 21 123 END
- gets the verb used to get a variable with a token
- VALUE is the value line
- counter is key
- 0 means zero flag
- 3 is the size of the value
- 21 is our magic token
- 123 is the value
- END means is the operation is successfull
We have made some operations and incremented the counter value to 124 and want to set the new value
cas counter 0 0 3 21 124 STORED
- cas the verb
- counter thekey we want to change
- 0 flag
- 0 timeout
- 3 size of the new value
- 21 token for this operation
- 124 new value
- STORED operation successfully complated