memc_cas(). So, I decided to create a new function, memc_get_cas(), which obtains the CAS value for an item so you can subsequently use this value in a memc_cas() call, which is shown in the example below:
mysql> select memc_get_cas('t1');
+--------------------+
| memc_get_cas('t1') |
+--------------------+
| 3 |
+--------------------+
1 row in set (0.00 sec)
mysql> select memc_get('t1');
+----------------+
| memc_get('t1') |
+----------------+
| new value |
+----------------+
1 row in set (0.00 sec)
mysql> select memc_cas('t1', 'change me', 1);
+--------------------------------+
| memc_cas('t1', 'change me', 1) |
+--------------------------------+
| 0 |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select memc_cas('t1', 'change me', 3);
+--------------------------------+
| memc_cas('t1', 'change me', 3) |
+--------------------------------+
| 1 |
+--------------------------------+
1 row in set (0.00 sec)
mysql> select memc_get('t1');
+----------------+
| memc_get('t1') |
+----------------+
| change me |
+----------------+
1 row in set (0.00 sec)
mysql> select memc_get_cas('t1');
+--------------------+
| memc_get_cas('t1') |
+--------------------+
| 4 |
+--------------------+
1 row in set (0.00 sec)
--### someone else makes a change with another client! ###
mysql> select memc_cas('t1', 'cas changed value yet again', 4);
+---------------------------------------------------+
| memc_cas('t1', 'cas changed value yet again', 93) |
+---------------------------------------------------+
| 0 |
+---------------------------------------------------+
1 row in set (0.00 sec)
mysql> select memc_get('t1');
+-------------------------+
| memc_get('t1') |
+-------------------------+
| someone else changed me |
+-------------------------+
1 row in set (0.00 sec)
mysql> select memc_get_cas('t1');
+--------------------+
| memc_get_cas('t1') |
+--------------------+
| 5 |
+--------------------+
1 row in set (0.00 sec)
As you can see, the CAS value for the item cached with the key 't1', 3 is returned. The call to
memc_get() returns the current value of 't1', 'new value'. Next, memc_cas() is called using the CAS value of 1, which does not work since it's not t1's current CAS value. However, the next call to memc_cas() uses 3 as the CAS value, which succeeds. Next, memc_get() shows the value has been changed by the previous memc_cas() call as well as memc_get_cas() showing that the CAS value has been incremented to 4. At this point, even though you don't see it, another client calls memc_set() for t1. This makes it so the subsequent call to memc_cas() using the CAS value of 4 fails because the CAS value for t1 has been incremented due to the other client's change. And the call to memc_get() and memc_get_cas() reveals that in fact the changes from the other client occurred and are the reason for memc_cas() not succeeding. To find the code, please visit: https://launchpad.net/memcached-udf
Enjoy!