<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Read-through cache callbacks</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcached.callbacks.result.html">Result callbacks</a></div> <div class="next" style="text-align: right; float: right;"><a href="memcached.sessions.html">Sessions support</a></div> <div class="up"><a href="memcached.callbacks.html">Callbacks</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="memcached.callbacks.read-through" class="section"> <h2 class="title">Read-through cache callbacks</h2> <p class="para"> 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 <span class="methodname"><a href="memcached.get.html" class="methodname">Memcached::get()</a></span> and <span class="methodname"><a href="memcached.getbykey.html" class="methodname">Memcached::getByKey()</a></span> support these callbacks, because the memcache protocol does not provide information on which keys were not found in the multi-key request. </p> <div class="example" id="example-4427"> <p><strong>Example #1 Read-through callback example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$m </span><span style="color: #007700">= new </span><span style="color: #0000BB">Memcached</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">addServer</span><span style="color: #007700">(</span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">, </span><span style="color: #0000BB">11211</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$profile_info </span><span style="color: #007700">= </span><span style="color: #0000BB">$m</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'user:'</span><span style="color: #007700">.</span><span style="color: #0000BB">$user_id</span><span style="color: #007700">, </span><span style="color: #DD0000">'user_info_cb'</span><span style="color: #007700">);<br /><br />function </span><span style="color: #0000BB">user_info_cb</span><span style="color: #007700">(</span><span style="color: #0000BB">$memc</span><span style="color: #007700">, </span><span style="color: #0000BB">$key</span><span style="color: #007700">, &</span><span style="color: #0000BB">$value</span><span style="color: #007700">)<br />{<br /> </span><span style="color: #0000BB">$user_id </span><span style="color: #007700">= </span><span style="color: #0000BB">substr</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/* lookup profile info in the DB */<br /> /* ... */<br /> </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">$profile_info</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">true</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="memcached.callbacks.result.html">Result callbacks</a></div> <div class="next" style="text-align: right; float: right;"><a href="memcached.sessions.html">Sessions support</a></div> <div class="up"><a href="memcached.callbacks.html">Callbacks</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>