【问题标题】:Memcache + Mysqli Couldn't fetch mysqli_resultMemcache + Mysqli 无法获取 mysqli_result
【发布时间】: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


【解决方案1】:

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

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

您需要存储查询的结果数据,即可以放入内存缓存的静态数据。

【讨论】:

    猜你喜欢
    • 2012-08-27
    • 2015-11-01
    • 2013-11-25
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多