How does addServer method of PHP Memcache/Memcached work?












2















I'm currently running PHP Memcache on Apache server. Since Memcache and Memcached have similar inner workings this question is about both of them.



I was wondering through the addServer method of memcached here and the second comment on the user section is this:




Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.




It is not clear what is meant by "every run". Does it mean calling addServer within the script multiple times or within multiple requests by different users/remote clients? Because consider the following script:



<?php
$memcache_obj = new Memcache;
//$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
$memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
//echo("There was a problem with {$hostname} at {$port}");
die;
});
print_r($memcache_obj->getExtendedStats());
?>


If as client, I make an xmlhttp request to above script, I will get something like this:



Array
(
[localhost:11211] => Array
(
[pid] => 12308
[uptime] => 3054538123
....


So far so good, if I uncomment the addServer part and execute like this:



<?php
$memcache_obj = new Memcache;
print_r($memcache_obj->getExtendedStats());
?>


Then I get this:



<br />
<b>Warning</b>: MemcachePool::getserverstatus(): No servers added to
memcache connection in <b>path/to/php</b> on line <b>someLineNumber</b><br />


So obviously at least a server has to be added when the php script is called by the remote client. Then which of the following is true here:




  • we should be careful not to call `addServer`` within the same PHP script too many times. (I am inclined to understand it this way)

  • we should be careful not to call addServer among multiple requests (For example 2 user's calling same php script etc. I can't seem to figure out how this can ever be done.)










share|improve this question


















  • 1





    Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

    – deceze
    Nov 24 '18 at 18:15
















2















I'm currently running PHP Memcache on Apache server. Since Memcache and Memcached have similar inner workings this question is about both of them.



I was wondering through the addServer method of memcached here and the second comment on the user section is this:




Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.




It is not clear what is meant by "every run". Does it mean calling addServer within the script multiple times or within multiple requests by different users/remote clients? Because consider the following script:



<?php
$memcache_obj = new Memcache;
//$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
$memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
//echo("There was a problem with {$hostname} at {$port}");
die;
});
print_r($memcache_obj->getExtendedStats());
?>


If as client, I make an xmlhttp request to above script, I will get something like this:



Array
(
[localhost:11211] => Array
(
[pid] => 12308
[uptime] => 3054538123
....


So far so good, if I uncomment the addServer part and execute like this:



<?php
$memcache_obj = new Memcache;
print_r($memcache_obj->getExtendedStats());
?>


Then I get this:



<br />
<b>Warning</b>: MemcachePool::getserverstatus(): No servers added to
memcache connection in <b>path/to/php</b> on line <b>someLineNumber</b><br />


So obviously at least a server has to be added when the php script is called by the remote client. Then which of the following is true here:




  • we should be careful not to call `addServer`` within the same PHP script too many times. (I am inclined to understand it this way)

  • we should be careful not to call addServer among multiple requests (For example 2 user's calling same php script etc. I can't seem to figure out how this can ever be done.)










share|improve this question


















  • 1





    Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

    – deceze
    Nov 24 '18 at 18:15














2












2








2








I'm currently running PHP Memcache on Apache server. Since Memcache and Memcached have similar inner workings this question is about both of them.



I was wondering through the addServer method of memcached here and the second comment on the user section is this:




Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.




It is not clear what is meant by "every run". Does it mean calling addServer within the script multiple times or within multiple requests by different users/remote clients? Because consider the following script:



<?php
$memcache_obj = new Memcache;
//$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
$memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
//echo("There was a problem with {$hostname} at {$port}");
die;
});
print_r($memcache_obj->getExtendedStats());
?>


If as client, I make an xmlhttp request to above script, I will get something like this:



Array
(
[localhost:11211] => Array
(
[pid] => 12308
[uptime] => 3054538123
....


So far so good, if I uncomment the addServer part and execute like this:



<?php
$memcache_obj = new Memcache;
print_r($memcache_obj->getExtendedStats());
?>


Then I get this:



<br />
<b>Warning</b>: MemcachePool::getserverstatus(): No servers added to
memcache connection in <b>path/to/php</b> on line <b>someLineNumber</b><br />


So obviously at least a server has to be added when the php script is called by the remote client. Then which of the following is true here:




  • we should be careful not to call `addServer`` within the same PHP script too many times. (I am inclined to understand it this way)

  • we should be careful not to call addServer among multiple requests (For example 2 user's calling same php script etc. I can't seem to figure out how this can ever be done.)










share|improve this question














I'm currently running PHP Memcache on Apache server. Since Memcache and Memcached have similar inner workings this question is about both of them.



I was wondering through the addServer method of memcached here and the second comment on the user section is this:




Important to not call ->addServers() every run -- only call it if no servers exist (check getServerList() ); otherwise, since addServers() does not check for dups, it will let you add the same server again and again and again, resultings in hundreds if not thousands of connections to the MC daemon. Specially when using FastCGI.




It is not clear what is meant by "every run". Does it mean calling addServer within the script multiple times or within multiple requests by different users/remote clients? Because consider the following script:



<?php
$memcache_obj = new Memcache;
//$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
$memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
//echo("There was a problem with {$hostname} at {$port}");
die;
});
print_r($memcache_obj->getExtendedStats());
?>


If as client, I make an xmlhttp request to above script, I will get something like this:



Array
(
[localhost:11211] => Array
(
[pid] => 12308
[uptime] => 3054538123
....


So far so good, if I uncomment the addServer part and execute like this:



<?php
$memcache_obj = new Memcache;
print_r($memcache_obj->getExtendedStats());
?>


Then I get this:



<br />
<b>Warning</b>: MemcachePool::getserverstatus(): No servers added to
memcache connection in <b>path/to/php</b> on line <b>someLineNumber</b><br />


So obviously at least a server has to be added when the php script is called by the remote client. Then which of the following is true here:




  • we should be careful not to call `addServer`` within the same PHP script too many times. (I am inclined to understand it this way)

  • we should be careful not to call addServer among multiple requests (For example 2 user's calling same php script etc. I can't seem to figure out how this can ever be done.)







php memcached php-5.6 libmemcached






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 17:41









ibrahim tanyalcinibrahim tanyalcin

2,3301314




2,3301314








  • 1





    Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

    – deceze
    Nov 24 '18 at 18:15














  • 1





    Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

    – deceze
    Nov 24 '18 at 18:15








1




1





Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

– deceze
Nov 24 '18 at 18:15





Given that it’s a user comment and not an official manual entry, and that it would be very hard to coordinate any implicit state kept between PHP instances, I’d take this with a huge grain of salt.

– deceze
Nov 24 '18 at 18:15












1 Answer
1






active

oldest

votes


















1














You do have to add the server once, else you will get this error. As the comment suggests you should use getServerList() to check if the servers have been added already and add them if they are not present:



<?php
$memcache_obj = new Memcache;
//$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
if (!memcache_obj->getServerList()){
$memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
//echo("There was a problem with {$hostname} at {$port}");
die;
});
}
print_r($memcache_obj->getExtendedStats());
?>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460798%2fhow-does-addserver-method-of-php-memcache-memcached-work%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You do have to add the server once, else you will get this error. As the comment suggests you should use getServerList() to check if the servers have been added already and add them if they are not present:



    <?php
    $memcache_obj = new Memcache;
    //$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
    if (!memcache_obj->getServerList()){
    $memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
    //echo("There was a problem with {$hostname} at {$port}");
    die;
    });
    }
    print_r($memcache_obj->getExtendedStats());
    ?>





    share|improve this answer




























      1














      You do have to add the server once, else you will get this error. As the comment suggests you should use getServerList() to check if the servers have been added already and add them if they are not present:



      <?php
      $memcache_obj = new Memcache;
      //$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
      if (!memcache_obj->getServerList()){
      $memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
      //echo("There was a problem with {$hostname} at {$port}");
      die;
      });
      }
      print_r($memcache_obj->getExtendedStats());
      ?>





      share|improve this answer


























        1












        1








        1







        You do have to add the server once, else you will get this error. As the comment suggests you should use getServerList() to check if the servers have been added already and add them if they are not present:



        <?php
        $memcache_obj = new Memcache;
        //$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
        if (!memcache_obj->getServerList()){
        $memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
        //echo("There was a problem with {$hostname} at {$port}");
        die;
        });
        }
        print_r($memcache_obj->getExtendedStats());
        ?>





        share|improve this answer













        You do have to add the server once, else you will get this error. As the comment suggests you should use getServerList() to check if the servers have been added already and add them if they are not present:



        <?php
        $memcache_obj = new Memcache;
        //$memcache_obj->connect('localhost', 11211); --> each time new connection, not recommended
        if (!memcache_obj->getServerList()){
        $memcache_obj->addServer('localhost', 11211,true,1,1,15,true,function($hostname,$port){
        //echo("There was a problem with {$hostname} at {$port}");
        die;
        });
        }
        print_r($memcache_obj->getExtendedStats());
        ?>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 8:40









        Sascha TrifunovicSascha Trifunovic

        1677




        1677
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460798%2fhow-does-addserver-method-of-php-memcache-memcached-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin