downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Memcache::addServer> <Fonctions Memcache
Last updated: Fri, 14 Aug 2009

view this page in

Memcache::add

(PECL memcache >= 0.2.0)

Memcache::addAjout d'un élément au serveur

Description

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

Memcache::add() enregistre la variable var avec la clé key seulement si cette clé n'existe pas encore sur le serveur. Vous pouvez également utiliser la fonction Memcache_add().

Liste de paramètres

key

La clé qui sera associée avec l'élément.

var

La variable à enregistrer. Les chaînes de caractères et les entiers sont enregistrés comme tels, les autres types sont enregistrés de manière sérialisée.

flag

Utilisez MEMCACHE_COMPRESSED pour enregistrer l'élément compressé (utilise zlib).

expire

Temps d'expiration pour l'élément. S'il égal 0, l'élément n'expirera jamais. Vous pouvez aussi utiliser un timestamp Unix ou un nombre de seconde en commençant par la date d'aujourd'hui, mais dans le dernier cas, le nombre de secondes ne doit pas excéder 2592000 (30 jours).

Valeurs de retour

Cette fonction retourne TRUE en cas de succès, FALSE en cas d'échec. Retourne FALSE si cette clé existe déjà. Pour le reste, la fonction Memcache::add() se comporte de façon similaire à la fonction Memcache::set().

Exemples

Exemple #1 Exemple avec Memcache::add()

<?php

$memcache_obj 
memcache_connect("localhost"11211);

/* API procédurale */
memcache_add($memcache_obj'var_key''test variable'false30);

/* API orientée objet */
$memcache_obj->add('var_key''test variable'false30);

?>

Voir aussi



Memcache::addServer> <Fonctions Memcache
Last updated: Fri, 14 Aug 2009
 
add a note add a note User Contributed Notes
Memcache::add
matt
04-Aug-2009 01:39
It's also good to note that add will succeed if the key exists but is expired
ktamas77 at gmail dot com
27-Apr-2009 11:08
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;

?>
conrad1986 at gmail dot com
18-Feb-2009 08:47
If you want to add an entry without using the locking method below (takes up extra space), try it this way:

<?php
if(!$mc->replace("key", $data))
   
$mc->set("key", $data;
?>

That way it will simply try to overwrite the value if a user has already (re)created the key. This is best in high-traffic sites where a user set()'s the data while another user has already tried a get() check and found nothing. Even this method still leaves a small microsecond window open for the same thing. However it helps prevent nasty glitches with submitting multiple entries after a get() check and the system has taken time to recompile the data to send to Memcached, after another user's request has processed this transaction.
rune(at)intermedia(dot)no
06-Oct-2008 06:04
Key may not exceed 250 chars according to memcached protocol.
php at tapirpirates dot net
03-Apr-2008 04:32
memcache has no locking mechanism, but you could implement it manually.

basic locking through the add method:

<?php
// locks time out after 5 seconds
Define( 'LOCK_TIMEOUT', 5 );

$lock = $memcache->add( 'lock:' . $key, 1, false, LOCK_TIMEOUT );
if (
$lock ) {
 
// no lock on this key, so do what you want
 
$value = $memcache->get( $key );
 
$memcache->set( $key, $value+1 );
 
// release lock
 
$memcache->delete( 'lock:' . $key );
}
else {
 
// variable is currently locked, so do something else
}
?>

furthermore, you could implement a loop which checks if there is a lock, and if there is, wait some time and try again, until the lock is free.

remember: locking will heavily increase your memcache hits and obviously is not what memcache is made for. altough it's not possible for a lock to be forgotten (there's a timeout after all) there is the possibility to get locked out for a very long time.

an alternative may be to implement locking through apc_add (or shared memory), but i've never tried it.

if you absolutley have to implement locks, memcached is probably the wrong solution anyway.
roberto at spadim,com dot br
18-Jun-2007 05:13
if you read source code for MMC_SERIALIZED you will see at line ~1555 that only !(is_string,is_long,is_double,is_bool) are serialized
and that serialized values are flaged as MMC_SERIALIZED for return (fetch) code unserialize these values again

Memcache::addServer> <Fonctions Memcache
Last updated: Fri, 14 Aug 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites