Home > Backend Development > PHP Tutorial > Memcached::cas detailed explanation

Memcached::cas detailed explanation

高洛峰
Release: 2016-10-17 10:20:50
Original
1253 people have browsed it

Memcached::cas

(PECL memcached >= 0.1.0)

Memcached::cas — Compare and exchange values ​​

Description

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

Memcached::cas() performs a "check and set" operation, therefore, it only obtains the value after the last time by the current client, and the value corresponding to the key has not been modified by other clients. Only in this case can the value be written. The check is performed through the cas_token parameter. This parameter is a unique 64-bit value assigned by Memcach to an existing element. For how to obtain this value, please check the documentation of the Memcached::get*() series of methods. Note: This value is of type double due to PHP's integer space limitations.

Translation Note: This is a very important advantage of Memcached extension over Memcache extension. With such a system-level (provided by Memcache itself) conflict detection mechanism (optimistic locking), we can ensure data security under high concurrency.

Parameter

cas_token

The unique value associated with the existing element, generated by Memcache.

key

The key name used to store the value.

value

The stored value.

expiration

Expiration time, default is 0. See Expiration Time for more information.

Return Value

Returns TRUE on success, or FALSE on failure. Memcached::getResultCode() will return Memcached::RES_DATA_EXISTS if an element is found to have been modified by another client since it was last retrieved by this client when trying to store it.

Example

Example #1 Memcached::cas() Example

<?php
$m = new Memcached();
$m->addServer(&#39;localhost&#39;, 11211);
do {
    /* 获取ip列表以及它的标记 */
    $ips = $m->get(&#39;ip_block&#39;, null, $cas);
    /* 如果列表不存在, 创建并进行一个原子添加(如果其他客户端已经添加, 这里就返回false)*/
    if ($m->getResultCode() == Memcached::RES_NOTFOUND) {
        $ips = array($_SERVER[&#39;REMOTE_ADDR&#39;]);
        $m->add(&#39;ip_block&#39;, $ips);
    /* 其他情况下,添加ip到列表中, 并以cas方式去存储, 这样当其他客户端修改过, 则返回false */
    } else { 
        $ips[] = $_SERVER[&#39;REMOTE_ADDR&#39;];
        $m->cas($cas, &#39;ip_block&#39;, $ips);
    }   
} while ($m->getResultCode() != Memcached::RES_SUCCESS);
?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template