热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpmemcached数据缓存入门例子

phpmemcached数据缓存入门例子:cachenewmemcache();youcanreplacelocalhostbymemcachedserveripaddrandpo
  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. ?>

推荐阅读
author-avatar
龙晓桥的微博_267
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有