略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 结果回调

2024-04-29

结果回调

Result callbacks方式在通过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)
}
add a noteadd a note

User Contributed Notes 1 note

up
3
edwarddrapkin at gmail dot com
13 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 :)

官方地址:https://www.php.net/manual/en/memcached.callbacks.result.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3