views:

183

answers:

2

Hi everyone,

I've got the following code:

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' );

The server is online and running (verified!), but the Failure callback function is being called at each connection. Why is that?

Reference:

PHP documentation: Memcache::addServer -> failure_callback

Allows the user to specify a callback function to run upon encountering an error. The callback is run before failover is attempted. The function takes two parameters, the hostname and port of the failed server

Edit: Updated post with correct number of parameters before the callback function, but without any luck :(

+3  A: 

The reason your code doesn't work is you aren't actually passing a callback. You're just calling that function and passing the return value. A callback in PHP is usually either a string with the function name or an array for object methods.

$this->memcache->addServer('192.168.1.35', '11211', 0, 50, 10, TRUE, array($this, '_call_memecache_failure');

The parameters should be passed by the function. You can learn more about how callbacks work in PHP in the documentation

Examples (from the docs):

<?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
?>
Chris T
Hi Chris! Thanks a lot for an extensive reply. However, it's not working as you say. I have tried to call the callback function with both your example ( array($this, '_call_memecache_failure') ) as well as what's shown at PHP.net ( '_call_memecache_failure' ). Do you have any ideas?
Industrial
Actually, straight from your excample, here's whats happen: Message: Memcache::addserver() expects parameter 7 to be boolean, array given
Industrial
Looks like it's missing an argument before the callback. Double check to make sure you're calling the function correctly (I just copied your function call and changed the callback). I think you're missing the $status argument or something. I've never used memecache so I don't know what the arguments mean
Chris T
Try addServer($host, $port, true, array($this, '_call_memecache');
Chris T
Hi! Thanks, I had missed an argument actually. Updated my original post now, but it still doesn't call the function at an offline server...
Industrial
Hi again, Nope, that didnt work either: Message: Memcache::addserver() expects parameter 4 to be long, array given
Industrial
+1  A: 

http://docs.php.net/memcache.addserver says:

Memcache::addServer
[...]
When using this method (as opposed to Memcache::connect() and Memcache::pconnect()) the network connection is not established until actually needed.
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);

prints (with no memcached running on localhost)

Foo::construct
  adding server
  constructor done
Foo::bar(1)
 ( Foo::failureCallback: memcache '127.0.0.1:11211' failed )
  add() returned false
VolkerK
Hi Volker! Thanks a lot for your reply. That made a lot of sense actually!
Industrial