PHP 8.4.1 Released!

Memcached::get

(PECL memcached >= 0.1.0)

Memcached::getObtener un ítem

Descripción

public Memcached::get(string $key, callable $cache_cb = ?, float &$cas_token = ?): mixed

Memcached::get() devuelve el ítem que fue previamente guardado bajo la clave dada por key. Si se encuentra el ítem y se proporciona la variable cas_token, esta contendrá el valor del token CAS para el ítem. Ver Memcached::cas() para saber cómo utilizar los token CAS. Se podrían especificar Retrollamadas de caché de lectura previa mediante el parámetro cache_cb.

Parámetros

key

La clave del ítem a obtener.

cache_cb

La retrollamada de caché de lectura previa o null.

cas_token

La variable donde guardar el token CAS.

Valores devueltos

Devuelve el valor almacenado 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 Otro ejemplo de 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 {
/* registrar el error */
/* ... */
}
}
?>

Ver también

add a note

User Contributed Notes 3 notes

up
12
letynsoft at gmail dot com
7 years ago
As of some version of php7 (i was not able to determine which exactly).
The $cas_token is no longer valid argument. It has been removed in favor of flags argument, as it appears to be causing issues when subclassing the Memcached class.

See https://github.com/php-memcached-dev/php-memcached/pull/214 for more details.

Basically you need to
<?php
function memcacheGet($key, $cb = null, &$cas = null) {
$m = memcacheGetObject();
if(empty(
$m))
return
false;
if(
defined('Memcached::GET_EXTENDED')) {
//Incompatible change in php7, took me 2 hours to figure this out, grrr
$_o = $m->get($key, $cb, Memcached::GET_EXTENDED);
$o = $_o['value'];
$cas = $_o['cas'];
} else {
$o = $m->get($key, $cb, $cas);
}
return
$o;
}
?>
up
10
miha at hribar dot info
15 years ago
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 :)
up
-2
denis_truffaut[A-T]hotmail[D-O-T]com
13 years ago
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 :)
To Top