Skip to content

Instantly share code, notes, and snippets.

@brianlmoon
Created April 9, 2015 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianlmoon/c4c61aaa77a259a9860d to your computer and use it in GitHub Desktop.
Save brianlmoon/c4c61aaa77a259a9860d to your computer and use it in GitHub Desktop.
Quickest, safe way to add servers using pecl-memcached and persistent connections.
<?php
// Let's assume this is coming from a config file somewhere
// that we can trust to not have duplicate servers in the list.
$servers = array(
array("host" => 'localhost', "port" => 11211, "weight" => 10),
array("host" => 'localhost', "port" => 11212, "weight" => 20)
);
$mc = new Memcached("test");
/**
* We simply check if this is a newly create object. If it is, we add the
* servers. The down side of this is if you want to remove connections to
* servers due to the server list changing, you will need to restart all
* your PHP workers.
*/
if($mc->isPristine()){
$mc->addServers($servers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment