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 :)
Memcached::get
(PECL memcached >= 0.1.0)
Memcached::get — Obtener un ítem
Descripción
Memcached::get() devuelve el ítem que fue previamente
guardado bajo la key. Si se encuentra el ítem y
se proporciona la variable cas_token, contendrá el valor
CAS token para el ítem. Ver
Memcached::cas() para ver como usar CAS tokens. Lectura sobre caché de llamadas de retorno se pueden
especificar mediante el parámetro cache_cb.
Parámetros
-
key -
La clave del ítem a obtener.
-
cache_cb -
Lectura caché de llamada de retorno o
NULL. -
cas_token -
La variable a guardar con el CAS token.
Valores devueltos
Devuelve el valor guardado en caché o FALSE en caso contrario.
Memcached::getResultCode() devolverá
Memcached::RES_NOTFOUND si la clave no existe.
Ejemplos
Ejemplo #1 Ejemplo 1 Memcached::get()
<?php
$m = new Memcached();
$m->addServer('localhost', 11211);
$m->set('foo', 100);
var_dump($m->get('foo'));
?>
El resultado del ejemplo sería:
int(100)
Ejemplo #2 Ejemplo 2 Memcached::get()
<?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 */
/* ... */
}
}
?>
Ver también
- Memcached::getByKey() - Obtiene un ítem de un servidor específico
- Memcached::getMulti() - Obtener múltiples ítems
- Memcached::getDelayed() - Obtener varios ítems
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 :)
