Memcache::add

(PECL memcache >= 0.2.0)

Memcache::addサーバーに項目を追加する

説明

Memcache::add(
    string $key,
    mixed $var,
    int $flag = ?,
    int $expire = ?
): bool

Memcache::add() は、サーバーに同名のキーが存在しない 場合に限り、key というキーで 値 var を格納します。 memcache_add() 関数を使用することも可能です。

パラメータ

key

項目に関連付けられたキー。

var

格納する値。文字列および整数値はそのままの形式で、それ以外の型は シリアライズされて格納されます。

flag

項目を圧縮して格納する場合に MEMCACHE_COMPRESSED を使用します (zlib を使用します)。

expire

項目の有効期限。ゼロの場合は有効期限なし (いつまでも有効) となります。Unix タイムスタンプ形式、あるいは現在からの 秒数で指定することが可能ですが、後者の場合は秒数が 2592000 (30 日) を超えることはできません。

戻り値

成功した場合に true を、失敗した場合に false を返します。 もし同名のキーが既に存在する場合は false を返します。 それ以外は、Memcache::add() の振る舞いは Memcache::set() と同じです。

例1 Memcache::add() の例

<?php

$memcache_obj
= memcache_connect("localhost", 11211);

/* 手続き型の API */
memcache_add($memcache_obj, 'var_key', 'test variable', false, 30);

/* オブジェクト指向の API */
$memcache_obj->add('var_key', 'test variable', false, 30);

?>

参考

add a note add a note

User Contributed Notes 5 notes

up
7
vasiliy at hotger dot com
10 years ago
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
up
4
Davide Renzi
14 years ago
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;
}
?>
up
4
ktamas77 at gmail dot com
14 years ago
skeleton of a thread safe updater for an incremental counter:

<?php

$key
= "counter";
$value = $memcache->increment($key, 1);
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 + 1, 0, 0);
   if (
$add_value === false) {
     
$value = $memcache->increment($key, 1)
      if (
$value === false) {
         
error_log ("counter update failed.");
      }
   } else {
     
$value = $db_value + 1;
   }
}

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

?>
up
1
matt
14 years ago
It's also good to note that add will succeed if the key exists but is expired
up
-2
roberto at spadim,com dot br
16 years ago
[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
To Top