我正在尝试为我正在使用内存缓存的项目增加一些速度性能。但是我遇到以下问题
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"
我错过了什么吗?我确信我以前以类似的方式使用过内存缓存,没有任何问题。
最佳答案
您不能将所有数据类型存储到 memcached(和其他数据存储)中,最常不起作用的是 resources Docs ,例如数据库链接或结果标识符。
您存储了这样一个值,并根据另一个请求再次将其取出。但由于数据库已经处于另一种状态,该资源不再起作用。然后你会得到错误。
相反,您需要存储查询的结果数据,即可以放入内存缓存中的静态数据。
关于php - Memcache + Mysqli 无法获取 mysqli_result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020678/