Memcached::cas

(PECL memcached >= 0.1.0)

Memcached::casCompare and swap an item

Описание

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

Memcached::cas() performs a "check and set" operation, so that the item will be stored only if no other client has updated it since it was last fetched by this client. The check is done via the cas_token parameter which is a unique 64-bit value assigned to the existing item by memcache. See the documentation for Memcached::get*() methods for how to obtain this token. Note that the token is represented as a double due to the limitations of PHP's integer space.

Список параметров

cas_token

Unique value associated with the existing item. Generated by memcache.

key

Ключ, под которым сохраняется значение.

value

Сохраняемое значение.

expiration

Время хранения объекта, по умолчанию равно 0. Для более подробной информации смотрите Время хранения объекта.

Возвращаемые значения

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки. The Memcached::getResultCode() will return Memcached::RES_DATA_EXISTS if the item you are trying to store has been modified since you last fetched it.

Примеры

Пример #1 Memcached::cas() example

<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);

do {
    
/* fetch IP list and its token */
    
$ips $m->get('ip_block'null$cas);
    
/* if list doesn't exist yet, create it and do
       an atomic add which will fail if someone else already added it */
    
if ($m->getResultCode() == Memcached::RES_NOTFOUND) {
        
$ips = array($_SERVER['REMOTE_ADDR']);
        
$m->add('ip_block'$ips);
    
/* otherwise, add IP to the list and store via compare-and-swap
       with the token, which will fail if someone else updated the list */
    
} else { 
        
$ips[] = $_SERVER['REMOTE_ADDR'];
        
$m->cas($cas'ip_block'$ips);
    }   
} while (
$m->getResultCode() != Memcached::RES_SUCCESS);

?>

Смотрите также

Коментарии

Watch out!

When using binary protocol, the expected result after cas() is 21 (Memcached::RES_END).

For example, to make the above example #1 work with binary protocol, use the following:
<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);
$m->setOption(Memcached::OPT_BINARY_PROTOCOL,true)

// [...]

   
} else { 
       
$ips[] = $_SERVER['REMOTE_ADDR'];
       
$m->cas($cas'ip_block'$ips);
    }   
} while (
$m->getResultCode() != Memcached::RES_END);
?>
2010-04-27 16:19:39
http://php5.kiev.ua/manual/ru/memcached.cas.html
Do not check command success in a while loop with something like

   
$memCached->getResultCode() != Memcached::RES_SUCCESS

Memcached::RES_SERVER_ERROR or anything like this and your script will loop forev
2016-10-11 19:11:51
http://php5.kiev.ua/manual/ru/memcached.cas.html
To prevent a perpetual loop on any Memcached error, you can add a simple counter :

$security_count = 0;

do {
        //[]....
        $security_loop++
        if ($security_loop > 10) {
                break;     //( or return "your return value" on a function )
        }
} while ($m->getResultCode() != Memcached::RES_SUCCESS);
2017-05-25 12:59:07
http://php5.kiev.ua/manual/ru/memcached.cas.html
Автор:
I'm not sure whether this remains true in the newer versions of the Memcached module (v3.0 onwards) but in the version shipped with PHP 5.6 the return value and result code when using this method with OPT_BINARY_PROTOCOL enabled are entirely useless.

Setting a value successful may return true, with a result code of RES_END, but it may also return true with a result code of RES_SUCCESS.

However, *unsuccessfully* setting a value likewise seems to return true and RES_SUCCESS, effectively rendering this function's return value useless with the binary protocol enabled as it is impossible to distinguish success from failure.

If you need to rely on the return value of this method then I strongly recommend disabling the binary protocol under PHP 5.6, as in its current state the common memcached module is too broken otherwise for CAS usage.

Hopefully someone else can weigh in on whether this is still broken in newer versions or not.
2017-08-18 22:26:15
http://php5.kiev.ua/manual/ru/memcached.cas.html

    Поддержать сайт на родительском проекте КГБ