我正在尝试为我正在使用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,没有任何问题。
最佳答案
不能将所有数据类型存储到memcached(和其他数据存储)中,其中一个通常不起作用的数据类型是resources ,例如数据库链接或结果标识符。
您存储了这样一个值,然后在另一个请求中再次将其取出。但由于数据库已处于另一种状态,此资源不再工作。你就会得到错误。
相反,您需要存储查询的结果数据,这是可以放入memcache的静态数据。