Memcache::add

(PECL memcache >= 0.2.0)

Memcache::addAdd an item to the server

Описание

bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )

Memcache::add() stores variable var with key only if such key doesn't exist at the server yet. Also you can use memcache_add() function.

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

key

The key that will be associated with the item.

var

The variable to store. Strings and integers are stored as is, other types are stored serialized.

flag

Use MEMCACHE_COMPRESSED to store the item compressed (uses zlib).

expire

Expiration time of the item. If it's equal to zero, the item will never expire. You can also use Unix timestamp or a number of seconds starting from current time, but in the latter case the number of seconds may not exceed 2592000 (30 days).

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

Возвращает TRUE в случае успешного завершения или FALSE в случае возникновения ошибки. Returns FALSE if such key already exist. For the rest Memcache::add() behaves similarly to Memcache::set().

Примеры

Пример #1 Memcache::add() example

<?php

$memcache_obj 
memcache_connect("localhost"11211);

/* procedural API */
memcache_add($memcache_obj'var_key''test variable'false30);

/* OO API */
$memcache_obj->add('var_key''test variable'false30);

?>

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

Коментарии

[c.2007]
if you read source code for MMC_SERIALIZED you will see at line ~1555 that [a line ~1560]
!(is_string,is_long,is_double,is_bool)

[is] serialized and that serialized values are flaged as MMC_SERIALIZED for return (fetch) code unserialize these values again
2007-06-18 01:13:19
http://php5.kiev.ua/manual/ru/memcache.add.html
skeleton of a thread safe updater for an incremental counter:

<?php

$key 
"counter";
$value $memcache->increment($key1);
if (
$value === false) {
   
// --- read from DB ---
   
$query "SELECT value FROM database";
   
$result mysql_query($query);
   
$row mysql_fetch_assoc($result);
   
$db_value $row["value"];
   
$add_value $memcache->add($key$db_value 100);
   if (
$add_value === false) { 
     
$value $memcache->increment($key1)
      if (
$value === false) {
         
error_log ("counter update failed.");
      }
   } else { 
     
$value $db_value 1;
   }
}

// --- display counter value ---
echo $value;

?>
2009-04-27 07:08:59
http://php5.kiev.ua/manual/ru/memcache.add.html
Автор:
It's also good to note that add will succeed if the key exists but is expired
2009-08-03 21:39:38
http://php5.kiev.ua/manual/ru/memcache.add.html
Автор:
Race conditions happen on an heavy load server when more than one thread tries to execute memcache_add.
For example if thread A and thread B try to save the same key you can test that sometimes both return TRUE.
To have the right behaviour you can verify that the correct value is in the assigned key:

<?php
function memcache_safeadd(&$memcache_obj$key$value$flag$expire)
{
    if (
memcache_add($memcache_obj$key$value$flag$expire))
    {
        return (
$value == memcache_get($memcache_obj$key));
    }
    return 
FALSE;
}
?>
2010-02-18 03:16:01
http://php5.kiev.ua/manual/ru/memcache.add.html
It looks like add() function is truly 100% atomic, and safeadd bicycle mentioned in the other comment is useless. There are few links where developers of Memcahed explained it deeper

http://lists.danga.com/pipermail/memcached/2008-March/006647.html
http://www.serverphorums.com/read.php?9,214222
2014-02-13 09:49:15
http://php5.kiev.ua/manual/ru/memcache.add.html

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