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'))
);
?>
Memcache::get
(PECL memcache:0.2-2.1.2)
Memcache::get — Récupère un élément du serveur de cache
Description
Memcache::get() retourne les données précédemment stockées dans l'élément identifié par la clé key s'il existe sur le serveur au moment de l'appel.
Vous pouvez passer un tableau de clés à la fonction Memcache::get() pour obtenir un tableau de valeurs. Le tableau résultant contiendra seulement les paires de clé-valeur trouvées.
Liste de paramètres
- key
-
La clé ou le tableau de clés à récupérer.
- flags
-
Si ce paramètre est présent, il représentera les drapeaux des valeurs à récupérer. Ces drapeaux sont les mêmes que ceux donnés en exemple de la fonction Memcache::set(). L'octet le plus faible de la valeur est réservé à un usage interne de pecl/memcache (e.g. pour indiquer le statut de compression et de linéarisation).
Valeurs de retour
Retourne une chaîne de caractères associée avec le paramètre key ou FALSE en cas d'échec ou si key n'a pas été trouvé.
Exemples
Exemple #1 Exemple avec Memcache::get()
<?php
/* API procédurale */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, 'some_key');
/* API orientée objet */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get('some_key');
/*
Vous pouvez également utiliser un tableau de clés en tant que paramètre.
Si un tel élément n'est pas trouvé sur le serveur, le tableau
résultat ne comprendra simplement pas une telle clé.
*/
/* API procédurale */
$memcache_obj = memcache_connect('memcache_host', 11211);
$var = memcache_get($memcache_obj, Array('some_key', 'another_key'));
/* API Orientée Objet */
$memcache_obj = new Memcache;
$memcache_obj->connect('memcache_host', 11211);
$var = $memcache_obj->get(Array('some_key', 'second_key'));
?>
Memcache::get
14-Oct-2008 05:18
30-Jul-2008 02:07
It looks like memcache take only first 256 characters. So if you want to cache some (large) queries do md5 or similar before caching.
07-Jul-2008 04:39
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.
