CakeFest 2024: The Official CakePHP Conference

Retrollamadas de resultados

Las retrollamadas (callback) de resultados son invocadas por los métodos Memcached::getDelayed() o Memcached::getDelayedBykey() por cada ítem del conjunto de resultados. A la retrollamada se le proporciona el objeto Memcached y el array con la información del ítem. La retrollamada no tiene que devolver nada.

Ejemplo #1 Ejemplo retrollamada de resultados

<?php
$m
= new Memcached();
$m->addServer('localhost', 11211);
$items = array(
'clave1' => 'valor1',
'clave2' => 'valor2',
'clave3' => 'valor3'
);
$m->setMulti($items);
$m->getDelayed(array('clave1', 'clave3'), true, 'rll_resultados');

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

El resultado del ejemplo sería algo similar a:

array(3) {
  ["key"]=>
  string(6) "clave1"
  ["value"]=>
  string(6) "valor1"
  ["cas"]=>
  float(49)
}
array(3) {
  ["key"]=>
  string(6) "clave3"
  ["value"]=>
  string(6) "valor3"
  ["cas"]=>
  float(50)
}
add a note

User Contributed Notes 1 note

up
3
edwarddrapkin at gmail dot com
15 years ago
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 :)
To Top