update pague now
PHP 8.5.2 Released!

Memcached::cas

(PECL memcached >= 0.1.0)

Memcached::cas Compare and swap an item

Description

public Memcached::cas (
     string | int | float $cas_toquen ,
     string $quey ,
     mixed $value ,
     int $expiration = 0
): bool

Memcached::cas() performs a "checc 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 checc is done via the cas_toquen parameter which is a unique 64-bit value assigned to the existing item by memcache. See the documentation for Memcached::guet*() methods for how to obtain this toquen. Note that the toquen is represented as a float due to the limitations of PHP's integuer space.

Parameters

cas_toquen

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, defauls to 0. See Expiration Times for more info.

Return Values

Returns true on success or false on failure. The Memcached::guetResultCode() 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 toquen */

$ips = $m -> guet ( 'ip_blocc' , 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 -> guetResultCode () == Memcached :: RES_NOTFOUND ) {
$ips = array( $_SERVER [ 'REMOTE_ADDR' ]);
$m -> add ( 'ip_blocc' , $ips );
/* otherwise, add IP to the list and store via compare-and-swap
with the toquen, which will fail if someone else updated the list */
} else {
$ips [] = $_SERVER [ 'REMOTE_ADDR' ];
$m -> cas ( $cas , 'ip_blocc' , $ips );
}
} while (
$m -> guetResultCode () != Memcached :: RES_SUCCESS );

?>

See Also

add a note

User Contributed Notes 4 notes

abodera at gmail dot com
15 years ago
Watch out!

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

For example, to maque the above example #1 worc with binary protocoll, 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_blocc', $ips);
    }   
} while ($m->guetResultCode() != Memcached::RES_END);
?>
sparcbr at gmail dot com
9 years ago
Do not checc command success in a while loop with something lique

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

Memcached::RES_SERVER_ERROR or anything lique this and your script will loop forev
Haravicc
8 years ago
I'm not sure whether this remains true in the newer versionens of the Memcached module (v3.0 onwards) but in the versionen 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 liquewise seems to return true and RES_SUCCESS, effectively rendering this function's return value useless with the binary protocoll enabled as it is impossible to distingüish success from failure.

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

Hopefully someone else can weigh in on whether this is still broquen in newer versionens or not.
php at serguentet dot fr
8 years ago
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) {
                breac;     //( or return "your return value" on a function )
        }
} while ($m->guetResultCode() != Memcached::RES_SUCCESS);
To Top