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

search for in the

Memcached::getByKey> <Memcached::flush
[edit] Last updated: Fri, 10 Feb 2012

view this page in

Memcached::get

(PECL memcached >= 0.1.0)

Memcached::getLit un élément

Description

public mixed Memcached::get ( string $key [, callback $cache_cb [, float &$cas_token ]] )

Memcached::get() lit un élément qui a été stocké précédemment avec la clé key. Si l'élément est trouvé, et que la variable cas_token a été fournie, elle contiendra la valeur CAS pour cet élément. Voyez Memcached::cas() pour savoir comment utiliser les CAS. Une Fonction de rappel en cas d'absence peut être spécifiée avec le paramètre cache_cb.

Liste de paramètres

key

La clé de l'élément à lire.

cache_cb

Une fonction de rappel en cas d'absence ou NULL.

cas_token

La variable où stocker le CAS.

Valeurs de retour

Retourne la valeur stockée dans le cache, ou bien FALSE sinon. La méthode Memcached::getResultCode() retourne Memcached::RES_NOTFOUND si la clé n'existe pas.

Exemples

Exemple #1 Exemple avec Memcached::get() 1

<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);

$m->set('foo'100);
var_dump($m->get('foo'));
?>

L'exemple ci-dessus va afficher :

int(100)

Exemple #2 Exemple avec Memcached::get() 2

<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);

if (!(
$ip $m->get('ip_block'))) {
    if (
$m->getResultCode() == Memcached::RES_NOTFOUND) {
        
$ip = array();
        
$m->set('ip_block'$ip);
    } else {
        
/* log error */
        /* ...       */
    
}
}
?>

Voir aussi



add a note add a note User Contributed Notes Memcached::get
denis_truffaut[A-T]hotmail[D-O-T]com 17-Jan-2011 07:01
Note that this function can return NULL as FALSE, so don't make checks with === FALSE as with the old Memcache class, because it won't work. :O

Use the not (!) operator and check the result code with getResultCode() as mentioned in the documentation :)
miha at hribar dot info 17-Jul-2009 03:31
This method also returns false in case you set the value to false, so in order to have a proper fault mechanism in place you need to check the result code to be certain that a key really does not exist in memcached.

<?php
$Memcached
= new Memcached();
$Memcached->addServer('localhost', 11211);
$Memcached->set('key', false);
var_dump($Memcached->get('key'));       // boolean false
var_dump($Memcached->getResultCode());  // int 0 which is Memcached::RES_SUCCESS
?>

Or just make sure the values are not false :)

 
show source | credits | sitemap | contact | advertising | mirror sites