©
本文档使用 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 ( $key , 5 );
$value = $profile_info ;
return true ;
}
?>
[#1] chadkouse [2011-12-27 13:24:30]
Or just set the value within the callback with your own custom expiration time and return false. I think it's cleaner.
[#2] oorza2k5 at gmail dot com [2009-03-20 17:40:23]
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.