note 99461 added to memcache.connect
| From: | geoffrey dot hoffman at gmail dot com | Date: | Wed, 18 Aug 2010 19:32:00 +0000 |
| Subject: | note 99461 added to memcache.connect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-171821@lists.php.net to get a copy of this message | ||
If memcached is working, calling memcache_connect( ) returns an Object instance, not a boolean. If
memcached is not working, calling memcache_connect( ) throws a notice AND a warning (and returns
false as expected).
<?php
/* memcache is running */
$test1 = memcache_connect('127.0.0.1',11211);
echo gettype($test1);
// object
echo get_class($test1);
// Memcache
/* memcached is stopped */
$test2 = memcache_connect('127.0.0.1',11211);
/*
Notice: memcache_connect(): Server 127.0.0.1 (tcp 11211) failed with: A connection attempt failed
because the connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
(10060) in C:\Program Files\Support Tools\- on line 1
Warning: memcache_connect(): Can't connect to 127.0.0.1:11211, A connection attempt failed
because the connected party did not properly respond after a period of time, or established
connection failed because connected host has failed to respond.
(10060) in C:\Program Files\Support Tools\- on line 1
*/
echo gettype($test2);
// boolean
echo $test2===false;
// 1
?>
There appears to be no way to check whether memcached is actually running without resorting to error
suppression:
<?php
$test3 = @memcache_connect('127.0.0.1',11211);
if( $test3===false ){
// memcached is _probably_ not running
}
?>
----
Server IP: 192.41.42.26
Probable Submitter: 70.166.218.198
----
Manual Page -- http://www.php.net/manual/en/memcache.connect.php
Edit -- https://master.php.net/note/edit/99461
Del: integrated -- https://master.php.net/note/delete/99461/integrated
Del: useless -- https://master.php.net/note/delete/99461/useless
Del: bad code -- https://master.php.net/note/delete/99461/bad+code
Del: spam -- https://master.php.net/note/delete/99461/spam
Del: non-english -- https://master.php.net/note/delete/99461/non-english
Del: in docs -- https://master.php.net/note/delete/99461/in+docs
Del: other reasons-- https://master.php.net/note/delete/99461
Reject -- https://master.php.net/note/reject/99461
Search -- https://master.php.net/manage/user-notes.php