首頁  >  文章  >  後端開發  >  php memcached数据缓存入门例子

php memcached数据缓存入门例子

WBOY
WBOY原創
2016-07-25 08:52:14943瀏覽
  1. class mycache
  2. {
  3. private $cache;
  4. function __construct()
  5. {
  6. $this->cache = new memcache();
  7. // you can replace localhost by memcached server ip addr and port no.
  8. $this->cache->connect('localhost', 10987);
  9. } // bbs.it-home.org
  10. function get_data($key)
  11. {
  12. $data = $this->cache->get($key);
  13. if($data != null)
  14. return $data;
  15. else
  16. {
  17. if($this->cache->getresultcode() == memcached::res_notfound)
  18. {
  19. //do the databse query here and fetch data
  20. $this->cache->set($key,$data_returned_from_database);
  21. }
  22. else
  23. {
  24. error_log('no data for key '.$key);
  25. }
  26. }
  27. }
  28. }
  29. $cache = mycache();
  30. $cache->get_data('foo');
  31. ?>
复制代码


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn