Memcache + Mysqli无法获取mysqli_result [英] Memcache + Mysqli Couldn't fetch mysqli_result

查看:93
本文介绍了Memcache + Mysqli无法获取mysqli_result的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为使用Memcache的项目增加一些速度性能.但是我在以下方面遇到了问题

I'm trying to add some speed performance to a project I'm working on using memcache. However I'm having a problem with the following

    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中放入了一些东西,但是当我将其撤回并使用它时,如果不是从缓存中获取它,我会收到此错误.

It is certainly putting something into memcached but I get this error when pulling it back out and use it as I would if it wasn't from the cache.

"Warning: mysqli_result::fetch_assoc(): Couldn't fetch mysqli_result"

我错过了什么吗?我敢肯定,以前我以类似的方式使用过内存缓存,没有任何问题.

Am I missing something? I'm sure I've used memcache before in a similar way without any issues.

推荐答案

您不能将所有数据类型存储到memcached(和其他数据存储)中,其中最不起作用的是

You can not store all data-types into memcached (and other data-stores), one which most often does not work are resources Docs, for example a database link or a result identifier.

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

You stored such a value and on another request you pulled it out again. But as the database already is in another state, this resource does not work any longer. You get the error then.

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

Instead you need to store the result data from the query, that's static data you can put into memcache.

这篇关于Memcache + Mysqli无法获取mysqli_result的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆