downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Sessions support> <Result callbacks
[edit] Last updated: Fri, 18 May 2012

view this page in

Read-through cache callbacks

Read-through cache callbacks are invoked when an item cannot be retrieved from the server. The callback is passed the Memcached object, the requested key, and the by-reference value variable. The callback is responsible for setting the value and returning true or false. If the callback returns true, Memcached will store the populated value on the server and return it to the original calling function. Only Memcached::get() and Memcached::getByKey() support these callbacks, because the memcache protocol does not provide information on which keys were not found in the multi-key request.

Beispiel #1 Read-through callback example

<?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);
    
/* lookup profile info in the DB */
    /* ... */
    
$value $profile_info;
    return 
true;
}
?>


Sessions support> <Result callbacks
[edit] Last updated: Fri, 18 May 2012
 
add a note add a note User Contributed Notes Read-through cache callbacks
chadkouse 27-Dec-2011 01: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 01: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 20-Mar-2009 05: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.

 
show source | credits | sitemap | contact | advertising | mirror sites