Beware, if there's a backslash present in the key name then the results are unpredictable and random.
I was generating keys like this:
$mc_key = get_called_class(). $_COOKIE['crumb'];
This works fine when get_called_class() returns CLASSNAME.
But then I began using namespaces, and get_class_class() of course returned NAMESPACE\CLASSNAME
Well that backslash sends Memcache into quite the tizzy.
No errors, mind you, just insanity.
Memcache::get
(PECL memcache >= 0.2.0)
Memcache::get — Obtener valor del servidor
Descripción
$key
[, int &$flags
] )$keys
[, array &$flags
] )
Memcache::get() devuelve los datos previamente almacenados si
el valor con la key existe en el servidor en este momento.
Se puede pasar un array de llaves a Memcache::get() para obtener un array de valores. El resultado del array contendrá solamente las parejas llave-valor encontradas.
Parámetros
-
key -
La llave o array de llaves a capturar.
-
flags -
Si está presente, las flags capturadas juntamente con los valores serán escritas en este parámetro. Estas flags son las mismas que las dadas por ejemplo en Memcache::set(). El byte más bajo del entero está reservado para uso interno de pecl/memcache (ej. para estado de compresión y serialización).
Valores devueltos
Devuelve la cadena asociada con la clave dada por key o
un array de pares clave-valor cuando key es un array.
Devuelve FALSE en caso de error, key no se encuentra, o
key es un array vacío.
Ejemplos
Ejemplo #1 Ejemplo de Memcache::get()
<?php
/* API por procedimientos */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, 'some_key');
/* API OO */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get('some_key');
/*
También se puede usar array de keys como parámetro.
Si el valor no fue encontrado en el servidor, el
resultado del array simplemente no contendrá dicha
clave.
*/
/* API por procedimientos */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, Array('some_key', 'another_key'));
/* API OO */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get(Array('some_key', 'second_key'));
?>
Be aware that when using the multi-key version, Memcache::get returns bool false if no servers are configured for the pool (and possibly if other errors occur as well while attempting to fetch). Also, Memcache class throws annoying warnings on every get/set/delete-type calls if you have no servers added to the pool.
The following snippet var_dump's bool false, not an empty array like you might expect.
<?php
$cache = new Memcache;
// no $cache->addServer calls (for example,
due to temporarily disabling use of cache)
// use @ symbol to ignore warning
var_dump(
@$cache->get(array('one', 'two'))
);
?>
If deserialization fails for some reason, that is when memcache server returned flag 1 set, but the value was not a correctly serialized PHP data,
then Memcache::get acts in a following way:
If it was called with a single key to retrieve, then a warning is raised, but since it was not actually a bug of a server, the warning says something confusing like "Memcached Server Error: null" and the function returns bool(false).
If it was called by passing an array (even with a single element in it), then the warning is not raised and the resulting array contains a value bool(false).
Since there are some buffer overrun bugs present in Memcached Server, which from time to time cause overwriting of [part of] data and therefore rendering it impossible to deserialize, make sure to check if the result of Memcache::get contains only string, or deserialized structure. If the result is bool,dobule or long, then something went wrong.
$flags stays untouched if $key was not found on the server, it's helpfull to determine if bool(false) was stored:
<?php
$memcache = new Memcache();
$memcache->set('test', false); //
$flags = false;
var_dump($memcache->get('test', $flags)); // bool(false)
var_dump($flags); // int(256) - changed to int
$memcache->delete('test');
$flags = false;
var_dump($memcache->get('test', $flags)); // bool(false)
var_dump($flags); // bool(false) - untouched
?>
For me it was the case that if such key doesn't exist, null is returned not false.
It looks like memcache take only first 256 characters. So if you want to cache some (large) queries do md5 or similar before caching.
