• PHP Video Tutorials

PHP - Memcache::addServer() Function



Memcache::addServer() function can add a memcached server to connection pool.

Syntax

bool Memcache::addServer( 
   string $host [, 
      int $port = 11211 [, 
         bool $persistent [, 
            int $weight [, 
               int $timeout [, 
                  int $retry_interval [, 
                     bool $status [, 
                        callable $failure_callback [, int $timeoutms ]
                     ]
                  ]
               ]
            ]
         ]
      ]
   ]
)

Memcache::addServer() function can add the server to connection pool. The connection that was opened using Memcache::addServer() function can be automatically closed at an end of script execution, we can also close it manually with Memcache::close() function. We can also use the memcache_add_server() function.

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

The failover may occur at any stage in any of the functions, as long as other servers are available the request and the user won't notice. Any kind of socket or Memcached server level errors (except out of memory) may trigger a failover. The normal client errors such as adding an existing key can't trigger a failover.

Memcache::addServer() function can return true on success or false on failure.

Example

<?php
   /* OO API */

   $memcache = new Memcache;
   $memcache->addServer('memcache_host', 11211);
   $memcache->addServer('memcache_host2', 11211);

   /* procedural API */

   $memcache_obj = memcache_connect('memcache_host', 11211);
   memcache_add_server($memcache_obj, 'memcache_host2', 11211);
?>
Advertisements