phpmemcached数据缓存入门例子
时间:2021-07-01 10:21:17
帮助过:25人阅读
- class mycache
- {
- private $cache;
- function __construct()
- {
- $this->cache = new memcache();
- // you can replace localhost by memcached server ip addr and port no.
- $this->cache->connect('localhost', 10987);
- } // bbs.it-home.org
-
- function get_data($key)
- {
- $data = $this->cache->get($key);
- if($data != null)
- return $data;
- else
- {
- if($this->cache->getresultcode() == memcached::res_notfound)
- {
- //do the databse query here and fetch data
- $this->cache->set($key,$data_returned_from_database);
- }
- else
- {
- error_log('no data for key '.$key);
- }
- }
- }
- }
- $cache = mycache();
- $cache->get_data('foo');
- ?>
|