the expiration time set for you if you return true from the callback will be 0 (forever) - so if you want a different expiration time you can do the SET operation inside the callback with your custom expiration time. just make sure you return FALSE from the callback to prevent the client from automatically setting the value again.
Fonctions de rappel sur clé absente
Les fonctions de rappel sur clé absente sont appelées quand un élément ne peut
pas être lu sur le serveur. La fonction de rappel reçoit un objet Memcached,
la clé demandée, et une valeur de variable par référence. La fonction de rappel
est alors responsable d'affecter la valeur, puis de retourner
TRUE ou FALSE. Si la fonction de rappel retourne TRUE
Memcached va stocker la valeur ainsi créée dans le serveur, et la retourner
à la fonction appelante. Seules Memcached::get() et
Memcached::getByKey() supportent ces fonctions,
car le protocole memcache ne fournit aucune information sur l'absence de
clé dans une requête multiclé.
Exemple #1 Fonctions de rappel sur clé absente
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$profile_info = $m->get('user:'.$user_id, 'user_info_cb');
function user_info_cb($memc, $key, &$value)
{
$user_id = substr($key, 5);
/* Lit un profil dans une base de données */
/* ... */
$value = $profile_info;
return true;
}
?>
chadkouse
27-Dec-2011 01:27
chadkouse
27-Dec-2011 01:24
Or just set the value within the callback with your own custom expiration time and return false. I think it's cleaner.
oorza2k5 at gmail dot com
20-Mar-2009 05:40
This isn't specified anywhere, so I had a gander at the source...
The expiry on read-through cache set values is set to 0, or forever. This means if you want your key to implicitly expire, don't use the callback methods, instead check for boolean false as a return and manually set the value, at least for now.
