Note that with this Memcached interface, if one or more underlying memcached daemons are down, suffering high network latency (over 1 second by default), or have crashed, then there still will be no warning, no error, no notice. Everything will appear to work correctly, you just won't get proper results back.
Memcached::__construct
(PECL memcached >= 0.1.0)
Memcached::__construct — Crear una instancia de Memcached
Descripción
Memcached::__construct()
([ string $persistent_id
] )
Crea una instancia de Memcached representando la conexión a los servidores de memcache.
Parámetros
- persistent_id
-
Por defecto las instancias de Memcached son destruidas al final de la petición. Para crear una instancia que persiste entre peticiones, use persistent_id para especificar un ID único para la instancia. Todas las instancias creadas con el mismo persistent_id compartirán la misma conexión.
Valores devueltos
El objeto Memcached.
Ejemplos
Example #1 Creando un objeto Memcached
<?php
/* Crea una instancia normal */
$m1 = new Memcached();
echo get_class($m);
/* Crea una instancia persistente */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');
/* ahora $m2 y $m3 comparten la misma conexión */
?>
Tobias
17-Feb-2011 03:24
Tobias
17-Feb-2011 11:48
Not positive, but it seems like the __construct() creates an instance of Memcache() class that lives (and stays alive) in at least the child Apache procs, if not the main Apache proc.
This (seems to) mean that Memcache daemons added to the server "pool" (via ->addServers() ) stay remembered.
This is why you do not want to call ->addServers() every time your script runs, because ->addServers() does not check for dups and will add hundreds or thousands of connections to the memcached daemon proc from the Apache proc(s).
One side effect is that any other scripts (even across different website domains) can also add to your server "pool" (if they know the server pool name, or if they can get it from the memcached daemon itself (not sure if/how this is possible), or if you or your team has coded several sites using the same server pool (i.e., new Memcached( 'same-server-pool-name' ) ). This may present a security hole.
jeroen at 4worx dot com
16-Nov-2009 10:51
Using multiple memcached instances with options in combination with persistent connections might be confusing:
<?php
$a = new Memcached('memcached_pool');
$a->setOption(Memcached::OPT_COMPRESSION, false);
$b = new Memcached('memcached_pool');
$b->setOption(Memcached::OPT_COMPRESSION, true);
$a->add('key', 'some data');
?>
You might think that connection $a will store everything uncompressed, but this is not the case.
The persistent connection options are changed by the second object creation.
tschundler at gmail dot com
15-Sep-2009 09:43
When using persistent connections, it is important to not re-add servers.
This is what you do not want to do:
<?php
$mc = new Memcached('mc');
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$mc->addServers(array(
array('mc1.example.com',11211),
array('mc2.example.com',11211),
));
?>
Every time the page is loaded those servers will be appended to the list resulting in many simultaneous open connections to the same server. The addServer/addServers functions to not check for existing references to the specified servers.
A better approach is something like:
<?php
$mc = new Memcached('mc');
$mc->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
if (!count($mc->getServerList())) {
$mc->addServers(array(
array('mc1.example.com',11211),
array('mc2.example.com',11211),
));
}
?>
