Heim  >  Artikel  >  Backend-Entwicklung  >  php memcached数据缓存入门例子

php memcached数据缓存入门例子

WBOY
WBOYOriginal
2016-07-25 08:52:14942Durchsuche
  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. ?>
复制代码


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn