Memcached optimistic lock implementation: CAS

Source: Internet
Author: User
Tags cas

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.