【发布时间】:2011-12-22 16:04:26
【问题描述】:
我正在尝试为我正在使用 memcache 处理的项目添加一些速度性能。但是我遇到以下问题
public function sql_query($query){
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$memkey = md5($query);
$get_result = $memcache->get($memkey);
if ($get_result){
return $get_result;
} else {
$q = $this->mysqli->query($query);
$memcache->set($memkey, $q, false, 120) or die ("Failed to save data at the server");
if ($this->mysqli->error){
App::Error($this->mysqli->error);
}
return $q;
}
}
它肯定会在 memcached 中放入一些东西,但是当我把它拉出来并使用它时,如果它不是来自缓存,我会得到这个错误。
"Warning: mysqli_result::fetch_assoc(): Couldn't fetch mysqli_result"
我错过了什么吗?我确信我以前以类似的方式使用过 memcache,没有任何问题。
【问题讨论】:
-
您应该缓存来自 mysqli 结果的记录,例如
$memcache->set($memkey, $q->fetch_all(), false, 120);.
标签: php mysql caching memcached