The Memcached::cas (check and set) uses optimistic locking to avoid overwriting modification issues that may occur when concurrent access occurs.
Returns TRUE
when successful, or on Failure FALSE
. Memcached::getresultcode () Memcached::RES_DATA_EXISTS
will return if the element is attempted to be stored and found to be modified by another client after the client's last fetch.
The following are examples of using do,while structures and CAs methods, if the CAs method
$m=NewMemcached ();$m-Addserver (' localhost ', 11211 ); Do { /*get the IP list and its tags*/ $ips=$m--Get (' Ip_block ',NULL,$cas ); /*If the list does not exist, create and make an atom addition (returns False if other clients have already added it)*/ if($m-Getresultcode () = = Memcached::res_notfound) { $ips=Array($_server[' REMOTE_ADDR ' ]); $mAdd (' Ip_block ',$ips ); /*in other cases, add the IP to the list and go to the CAs to store it, so that when other clients have modified it, it returns false*/ } Else { $ips[] =$_server[' REMOTE_ADDR ' ]; $m-CAS ($cas, ' Ip_block ',$ips ); } } while($m-Getresultcode ()! = Memcached::res_success);
Memcached optimistic lock implementation: CAS