回调
在线手册:中文 英文
PHP手册

通读缓存回调

通读缓存回调在一个元素没有从服务端检索到的时候被调用。这个回调函数会接收到Memcached对象,请求的key以及 一个引用方式传递的值变量等三个参数。此回调函数负责通过返回true或false来决定在key没有值时设置一个默认值。 如果回调返回true,Memcached会存储"传出参数"(引用传递的值变量)存储的值到memcached服务端并将其返回到原来 的调用函数中。仅仅 Memcached::get()Memcached::getByKey() 支持这类回调,因为Memcache协议不支持在请求多个key时提供未检索到key的信息。

Example #1 通读回调示例

<?php
$m 
= new Memcached();
$m->addServer('localhost'11211);

$profile_info $m->get('user:'.$user_id'user_info_cb');

function 
user_info_cb($memc$key, &$value)
{
    
$user_id substr($key5);
    
/* 从数据库读取个人信息 */
    /* ... */
    
$value $profile_info;
    return 
true;
}
?>

回调
在线手册:中文 英文
PHP手册
PHP手册 - N: 通读缓存回调

用户评论:

chadkouse (27-Dec-2011 09:27)

the expiration time set for you if you return true from the callback will be 0 (forever) - so if you want a different expiration time you can do the SET operation inside the callback with your custom expiration time.  just make sure you return FALSE from the callback to prevent the client from automatically setting the value again.

chadkouse (27-Dec-2011 09:24)

Or just set the value within the callback with your own custom expiration time and return false.  I think it's cleaner.

oorza2k5 at gmail dot com (21-Mar-2009 01:40)

This isn't specified anywhere, so I had a gander at the source...

The expiry on read-through cache set values is set to 0, or forever.  This means if you want your key to implicitly expire, don't use the callback methods, instead check for boolean false as a return and manually set the value, at least for now.