2024 年日本 PHP 研討會

結果回呼

結果 可呼叫物件會被 Memcached::getDelayed()Memcached::getDelayedBykey() 方法針對結果集中的每個項目呼叫。該回呼會傳入 Memcached 物件和包含項目資訊的陣列。回呼函式不需要回傳任何值。

範例 #1 結果回呼範例

<?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);
}
?>

上述範例將會輸出類似以下的內容:

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)
}
新增筆記

使用者貢獻的筆記 1 則筆記

edwarddrapkin at gmail dot com
15 年前
我在使用 getDelayed 中的結果回呼來呼叫方法時遇到問題,所以我寄信給了開發者。

如果您想使用非靜態方法作為回呼,請使用以下格式:array($obj, 'method'); 例如:

<?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();
?>

或者,另一種方法是:

<?php
類別 foo {
公開
$M = false;

公開 函式
__construct() {
$this->M = new Memcached();
$this->M->addServer('localhost', 11211);
$this->M->set('a', 'test');
}

公開 函式
fun() {
echo
"Great Success!";
}
}

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

運作良好,感謝 Andrei :)
To Top