PHP Extension Module memcached Long connection usage analysis, extension Module memcached_php tutorial

Source: Internet
Author: User

PHP Extension Module memcached long connection using method analysis, Extension module memcached


Online widely circulated an article, about PHP two extension modules memcache and memcached differences, which specifically emphasized the memcached and memcached a big difference is that the memcached module does not support long connections. So many years later I think memcached is not support long connection, in fact, the memcached extension module has been supported since the early version of the long connection. From the source gaze of the extension module we can see:

/* {{memcached::__construct ([string persistent_id[, Callback on_new[, String Connection_str]])

Creates a Memcached object, optionally using persistent memcache connection */

Static Php_method (Memcached, __construct)

{

From the PHP manual we can see that the constructor provided by the Memcached Extension module provides an optional persistent_id parameter , as described in the manual:

By default, memcached instances are destroyed after the request is completed. However, you can share an persistent_id instance between requests at creation time by specifying a unique ID for each instance. All instances created with persistent_id the same value share the same connection.

The implication of this parameter is that if you pass a named ID to the construction method, a long connection is established, usually we use the PHP-FPM mode, so that the PHP-FPM process will have a long connection channel to the Memcached service resume. We can also understand that persistent_id is a connection pool name, and all PHP-FPM processes are part of this pool of connections.

But we need to note that PHP is an explanatory language, when PHP first through the memached module to establish a long connection, it is important to remember that subsequent PHP execution will not build the same persistent_id named long connection through the Memcached constructor, Can establish a long connection of different persistent_id names, if the same name is repeated by PHP, it will lead to PHP-FPM process anomalies resulting in the communication with memcached more and more slowly, It also causes PHP to generate Coredump depending on the version of Libmemcached.

So how do we prevent a single php-fpm from repeating a long connection after it has been established with a long connection named persistent_id? In fact, in PHP with commentary on the manual is explained, the content is as follows:

When using persistent connections, it's important to not re-add servers.

This is the want to do:

$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 would be appended to the list resulting in many simultaneous open connections T o the same server. The Addserver/addservers functions to is check for existing references to the specified servers.

A better approach is something like:

$MC = new Memcached (' MC '); $MC->setoption (Memcached::opt_libketama_compatible, true); if (!count ($MC Getserverlist ())) {  $MC->addservers (Array ('    mc1.example.com ', 11211),    Array (' Mc2.example.com ', 11211),  ));}

Use the Getserverlist () method to check whether a long connection resource with the same name already exists in the PHP-FPM process container that is currently executing, and do not reuse the Addservers () method to grow the connection configuration if it exists.

http://www.bkjia.com/PHPjc/932496.html www.bkjia.com true http://www.bkjia.com/PHPjc/932496.html techarticle PHP Extension Module memcached Long connection usage analysis, expansion module memcached Online widely circulated an article about PHP two extension modules memcache and memcached the difference, especially ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.