<table cellspacing="0" cellpadding="0"><tr><td class="t_f" id="postmessage_47934"> <div class="blockcode"> <div id="code_EwC"><ol> <li> <li><?php <li>class mycache <li>{ <li> private $cache; <li> function __construct() <li> { <li> $this->cache = new memcache(); </li> <li> // you can replace localhost by memcached server ip addr and port no. </li> <li> $this->cache->connect('localhost', 10987); </li> <li> } // bbs.it-home.org</li> <li> </li> <li> function get_data($key) </li> <li> { </li> <li> $data = $this->cache->get($key); </li> <li> if($data != null) </li> <li> return $data; </li> <li> else </li> <li> { </li> <li> if($this->cache->getresultcode() == memcached::res_notfound) </li> <li> { </li> <li> //do the databse query here and fetch data </li> <li> $this->cache->set($key,$data_returned_from_database); </li> <li> } </li> <li> else </li> <li> { </li> <li> error_log('no data for key '.$key); </li> <li> } </li> <li> } </li> <li> } </li> <li>} </li> <li>$cache = mycache(); </li> <li>$cache->get_data('foo'); </li> <li>?></li> </ol></div> <em onclick="copycode($('code_EwC'));">复制代码</em> </div> </td></tr></table> <div id="comment_47934" class="cm"> </div> <div id="post_rate_div_47934"></div> <br><br>