PHP Doku:: Result callbacks - memcached.callbacks.result.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzSonstige DiensteMemcachedCallbacksResult callbacks

Ein Service von Reinhard Neidl - Webprogrammierung.

Callbacks

<<Callbacks

Read-through cache callbacks>>

Result callbacks

Result callbacks are invoked by Memcached::getDelayed() or Memcached::getDelayedBykey() methods for each item in the result set. The callback is passed the Memcached object and the array with the item information. The callback does not have to return anything.

Beispiel #1 Result callback example

<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);
$items = array(
    
'key1' => 'value1',
    
'key2' => 'value2',
    
'key3' => 'value3'
);
$m->setMulti($items);
$m->getDelayed(array('key1''key3'), true'result_cb');

function 
result_cb($memc$item)
{
    
var_dump($item);
}
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

array(3) {
  ["key"]=>
  string(4) "key1"
  ["value"]=>
  string(6) "value1"
  ["cas"]=>
  float(49)
}
array(3) {
  ["key"]=>
  string(4) "key3"
  ["value"]=>
  string(6) "value3"
  ["cas"]=>
  float(50)
}

Ein BenutzerBeitrag:
- Beiträge aktualisieren...
edwarddrapkin at gmail dot com
12.03.2009 2:54
I was having trouble making method calls with the result callbacks in getDelayed, so I emailed the developer.

If you want to use a non-static method as a callback, use the following format: array($obj, 'method'); for example:

<?php
class foo {
    private
$M = false;
   
    public function
__construct() {
       
$this->M = new Memcached();
       
$this->M->addServer('localhost', 11211);       
       
$this->M->set('a', 'test');
    }

    public function
test() {
       
$this->M->getDelayed(array('a'), false, array($this, 'fun'));
    }
   
    public function
fun() {
        echo
"Great Success!";
    }
}

$f = new foo();
$f->test();
?>

or, alternatively:

<?php
class foo {
    public
$M = false;
   
    public function
__construct() {
       
$this->M = new Memcached();
       
$this->M->addServer('localhost', 11211);       
       
$this->M->set('a', 'test');
    }
   
    public function
fun() {
        echo
"Great Success!";
    }
}

$f = new foo();
$f->M->getDelayed(array('a'), false, array($f, 'fun'));
?>

Works great, thanks Andrei :)



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)