Memcached Check and Set Operation with Python Example

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.

1

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

1

2

3

add counter3

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.

1

2

3

4

gets counter

VALUE counter321

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