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.
Memcached::__construct
(PECL memcached >= 0.1.0)
Memcached::__construct — Memcached のインスタンスを作成する
説明
Memcached::__construct
([ string $persistent_id
] )
memcache サーバとの接続を表す Memcached インスタンスを作成します。
パラメータ
- persistent_id
-
デフォルトでは、Memcached のインスタンスはリクエストの終了時に破棄されます。 リクエスト間で持続するインスタンスを作成するには、 persistent_id でそのインスタンスの一意な ID を指定します。 同じ persistent_id で作られたすべてのインスタンスは同じ接続を共有します。
返り値
Memcached オブジェクトを返します。
例
例1 Memcached オブジェクトの作成
<?php
/* 通常のインスタンスを作成します */
$m1 = new Memcached();
echo get_class($m);
/* 持続するインスタンスを作成します */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');
/* これで $m2 と $m3 は同じ接続を共有するようになります */
?>
Memcached::__construct
jeroen at 4worx dot com
16-Nov-2009 07:51
16-Nov-2009 07:51
tschundler at gmail dot com
15-Sep-2009 06:43
15-Sep-2009 06: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),
));
}
?>
