Maison  >  Article  >  développement back-end  >  php memcached数据缓存入门例子

php memcached数据缓存入门例子

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


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn