Memcached::cas detailed

Source: Internet
Author: User
Memcached::cas

(PECL memcached >= 0.1.0)

Memcached::cas-Comparing and exchanging values

Description

public bool Memcached::cas (float $cas _token, string $key, mixed $value [, int $expiration])

Memcached::cas () performs a "check and set" operation, so it can write the value only after the current client has the last value, and the value corresponding to the key is not modified by another client. The check is done through the Cas_token parameter, which is a unique 64-bit value assigned to the element that already exists, and how to get this value see the documentation for the memcached::get* () series method. Note: This value as a double type is due to the integer space limit of PHP.

This is a very important advantage of memcached extension than memcache extension, in such a system level (Memcache itself provides) the conflict detection mechanism (optimistic lock), we can guarantee high concurrency of data security.

Parameters

Cas_token

A unique value associated with an existing element, generated by memcache.

Key

The key name used to store the value.

Value

The stored value.

Expiration

The expiry time, which defaults to 0. For more information, see expiry time.

return value

Returns TRUE on success, or FALSE on failure. Memcached::getresultcode () returns memcached::res_data_exists if the element is attempted to store and is found to be modified by another client after the client's last fetch.

Example

Example #1 Memcached::cas () example

 
  Addserver (' localhost ', 11211);d o {    /* Gets 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) *    /if ($m->getresultcode () = = Memcached::res_notfound) {        $ ips = Array ($_server[' remote_addr ');        $m->add (' Ip_block ', $ips);    /* Other cases, add IP to the list and store it as CAs, 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);? >
  • Related Article

    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.