Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php memcached数据缓存入门例子

php memcached数据缓存入门例子

WBOY
WBOYasal
2016-07-25 08:52:14943semak imbas
  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. ?>
复制代码


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn