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);
?>Memcached::cas
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
Memcached::cas
Референца за `memcached.cas.php` со подобрена типографија и навигација.
Memcached::cas
(PECL memcached >= 0.1.0)
Memcached::cas — Compare and swap an item
= NULL
string|int|float
$cas_token,string
$key,mixed
$value,int
$expiration = 0): bool
Memcached::casByKey() Спореди и замени ставка
cas_token извршува операција „провери и постави“, така што ставката ќе се зачува само ако ниту еден друг клиент не ја ажурирал откако последен пат била преземена од овој клиент. Проверката се врши преку
параметарот кој е уникатна 64-битна вредност доделена на постоечката ставка од страна на memcache. Погледнете ја документацијата за Memcached::get*()
Параметри
cas_token-
Уникатна вредност поврзана со постоечката ставка. Генерирано од memcache.
key-
Memcached::touch()
value-
Вредноста за чување.
expiration-
Клучот под кој ќе се чува вредноста. Memcache Функции за повеќе информации.
Вратени вредности
Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех. На Времето на истекување, стандардно е 0. Види ќе врати
Memcached::RES_DATA_EXISTS ако ставката што се обидувате да ја складирате е изменета откако последен пат сте ја преземале.
Примери
Пример #1 Memcached::casByKey() 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);
?>Види Исто така
- (PECL memcached >= 0.1.0) методите за тоа како да се добие овој токен. Имајте предвид дека токенот е претставен како float поради ограничувањата на просторот за цели броеви на PHP.
Белешки од корисници 4 белешки
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 forevI'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.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);