note 106786 added to memcache.connect

From: Date: Tue, 06 Dec 2011 15:52:30 +0000
Subject: note 106786 added to memcache.connect
Groups: php.notes 
Request: Send a blank email to php-notes+get-184938@lists.php.net to get a copy of this message
There is a not-so-obvious way to check whether or not a MemCache-Server is available. Using ($memCache->connect() == false) will wait for a timeout if it can't connect. If you got a high-traffic site this may not be an option. So when the server is down, you may want to avoid waiting for this timeout on every request and instead try to reconnect only once every X seconds. If so, this code may help: <?php $memCache = new \Memcache(); $memCache->addServer($host, $port); $stats = @$memCache->getExtendedStats(); $available = (bool) $stats["$host:$port"]; if ($available && @$memCache->connect($host, $port)) // MemCache is there else // go on without MemCache ?> The result of getExtendedStats() is an array. The information is cached and maintained by MemCache itself. If the server is not available, the result will be FALSE. Even if the result is not false, the server may still not be available. Thus you need to check for connect() != false too, but only if the first check returns TRUE, thus avoiding the 1 second timeout in most cases. Both getExtendedStats() and connect() issue notices/warnings if the server is not there. Thus you have to mute both calls. Do NOT use getServerStatus() for this purpose: the result is cached on server-start and will not recognize when the connection to the server is lost (or reestablished) in between. ---- Server IP: 69.147.83.197 Probable Submitter: 193.227.194.40 ---- Manual Page -- http://www.php.net/manual/en/memcache.connect.php Edit -- https://master.php.net/note/edit/106786 Del: integrated -- https://master.php.net/note/delete/106786/integrated Del: useless -- https://master.php.net/note/delete/106786/useless Del: bad code -- https://master.php.net/note/delete/106786/bad+code Del: spam -- https://master.php.net/note/delete/106786/spam Del: non-english -- https://master.php.net/note/delete/106786/non-english Del: in docs -- https://master.php.net/note/delete/106786/in+docs Del: other reasons-- https://master.php.net/note/delete/106786 Reject -- https://master.php.net/note/reject/106786 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184938) next »