我有以下代码:
function failureCallback($host, $port) {
print "memcache '$host:$port' failed";
}
$this->memcache = new Memcache;
## bool Memcache::addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] )
$this->memcache->addServer('192.168.1.35', '11211', FALSE, 50, 10, 10, TRUE, 'failureCallback' );
服务器在线并正在运行(已验证!),但在每个连接上都会调用Failure回调函数。那是为什么?
参考:
PHP documentation: Memcache :: addServer - > failure_callback
允许用户指定回调 在遇到一个人时运行的功能 错误。之前运行回调 尝试进行故障转移。功能 有两个参数,主机名和 故障服务器的端口
编辑:在回调函数之前使用正确数量的参数更新了帖子,但没有任何运气:(
答案 0 :(得分:3)
您的代码不起作用的原因是您实际上没有传递回调。您只是调用该函数并传递返回值。 PHP中的回调通常是带有函数名的字符串或对象方法的数组。
$this->memcache->addServer('192.168.1.35', '11211', 0, 50, 10, TRUE, array($this, '_call_memecache_failure');
参数应该由函数传递。您可以在PHP in the documentation
中详细了解回调的工作原理示例(来自文档):
<?php
// An example callback function
function my_callback_function() {
echo 'hello world!';
}
// An example callback method
class MyClass {
static function myCallbackMethod() {
echo 'Hello World!';
}
}
// Type 1: Simple callback
call_user_func('my_callback_function');
// Type 2: Static class method call
call_user_func(array('MyClass', 'myCallbackMethod'));
// Type 3: Object method call
$obj = new MyClass();
call_user_func(array($obj, 'myCallbackMethod'));
// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');
// Type 5: Relative static class method call (As of PHP 5.3.0)
class A {
public static function who() {
echo "A\n";
}
}
class B extends A {
public static function who() {
echo "B\n";
}
}
call_user_func(array('B', 'parent::who')); // A
?>
答案 1 :(得分:1)
http://docs.php.net/memcache.addserver说:
内存缓存:: addServer
[...]
使用此方法时(与Memcache :: connect()和Memcache :: pconnect()相反),在实际需要之前不会建立网络连接。
class Foo {
protected $memcache;
public function __construct() {
echo "Foo::construct\n";
$this->memcache = new Memcache;
echo " adding server\n";
$this->memcache->addServer('127.0.0.1', 11211, false, 50, 5, 5, true, array($this, 'failureCallback'));
echo " constructor done\n";
}
public function bar($value) {
echo "Foo::bar($value)\n";
$b = $this->memcache->add('testkey', $value, false, 5);
echo ' add() returned ', $b ? 'true':'false', "\n";
}
public function failureCallback($host, $port) {
echo " ( Foo::failureCallback: memcache '$host:$port' failed )\n";
}
}
$foo = new Foo;
$foo->bar(1);
打印(没有在localhost上运行memcached)
Foo::construct
adding server
constructor done
Foo::bar(1)
( Foo::failureCallback: memcache '127.0.0.1:11211' failed )
add() returned false
答案 2 :(得分:0)
$this->memcache = new Memcache;
$this->memcache->addserver(
$host,
$port,
$persistent,
$weight,
$timeout,
$retryInterval,
$status,
function($host, $tcpPort, $udpPort, $errorMessage, $errorCode) {
$message = strtr('Memcache fail on :host (TCP::tcp/UDP::udp) with message ":msg" (:code)', [
':host' => $host,
':tcp' => $tcpPort,
':udp' => $udpPort,
':msg' => $errorMessage,
':code' => $errorCode
]);
error_log($message);
}
);