memcache connect vs addServer

前端 未结 3 2062
北荒
北荒 2021-02-20 05:12

I was looking at the php docs on memcache and noticed that instead of doing
$mem->connect(\'localhost\', 11211) I can do instead
$mem->addServer

3条回答
  •  孤城傲影
    2021-02-20 05:42

    The difference is actually explained quite well on the docs page for Memcache::addServer

    connect() makes a direct connection to a single instance of memcached.

    addServer() adds an entry to the server pool that the Memcache extension uses, but does not actually connect until you do something that requires a connection.

    When using this method (as opposed to Memcache::connect() and Memcache::pconnect()) the network connection is not established until actually needed. Thus there is no overhead in adding a large number of servers to the pool, even though they might not all be used.

    Using a server pool means that if one memcached instance/connection throws a low level error the Memcache extension will automatically connect to another server to make the request.

提交回复
热议问题