Memcached::cas

(PECL memcached >= 0.1.0)

Memcached::casCompare and swap an item

Description

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

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);

?>

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Definition of stored procedures - an introduction

Stored procedures are predefined SQL code blocks that are stored in a database and can be called up as required. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial