Memcache + Mysqli无法获取mysqli_result

时间:2011-11-05 14:09:57

标签: php mysql caching memcached

我正在尝试为我正在使用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而没有任何问题。

1 个答案:

答案 0 :(得分:3)

您无法将所有数据类型存储到memcached(和其他数据存储)中,最常见的是resources Docs,例如数据库链接或结果标识符。

您存储了这样的值,并在另一个请求中再次将其拉出。但由于数据库已处于另一个状态,因此该资源不再起作用。那你得到错误。

相反,您需要存储查询中的结果数据,这是您可以放入内存缓存的静态数据。