Memcached::cas
(PECL memcached >= 0.1.0)
Memcached::cas — Compare and swap an item
Description
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.
Parameters
-
cas_token
-
Unique value associated with the existing item. Generated by memcache.
-
key
-
The key under which to store the value.
-
value
-
The value to store.
-
expiration
-
The expiration time, defaults to 0. See Expiration Times for more info.
Return Values
Returns TRUE
on success or FALSE
on failure.
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.
Examples
Example #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);
?>
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-memcached.cas.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.