Memcache + Mysqli Couldn't fetch mysqli_result

梦想与她 提交于 2019-12-24 08:05:37

问题


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;
    }

}

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.


回答1:


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.



来源:https://stackoverflow.com/questions/8020678/memcache-mysqli-couldnt-fetch-mysqli-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!