Php Memcache和Mysqli准备好的语句问题

Php Memcache和Mysqli准备好的语句问题,php,mysqli,memcached,Php,Mysqli,Memcached,这让我抓狂,问题是我无法确定如何获取和设置要在视图中显示的缓存数据 public function get_something($id, $account_name) { $sql = "SELECT one,two,three FROM table WHERE id = ? and account_name = ? "; $key = md5("SELECT one,two,three FROM table WHERE id = $id and account_name = $

这让我抓狂,问题是我无法确定如何获取和设置要在视图中显示的缓存数据

public function get_something($id, $account_name)
{
    $sql = "SELECT one,two,three FROM table WHERE id = ? and account_name = ? ";
    $key = md5("SELECT one,two,three FROM table WHERE id = $id and account_name = $account_name ");

    $get_result = $this->Core->Core->Memcache->get($key);

    if($get_result)
    {
      // How would I set the Data
    }
    else
    {
     $stmt = $this->Core->Database->prepare($sql);
     $stmt->bind_param("is", $id, $account_name);
     $stmt->execute();
     $stmt->store_result();
     $stmt->bind_result($one, $two, $three);
     $stmt->fetch();
     //Below is how i set the data
     $this->Core->Template->set_data('one', $one);  
     //Set the Memcache
     $this->Core->Memcache->set($key, $stmt, TRUE, 20);
}

因此,我的问题是如何从memcache中的预处理语句fetch中获取和设置数据

Memcache是一个密钥/值存储系统,密钥和值都需要序列化。从页面:

请记住,资源变量(即文件和连接描述符)不能存储在缓存中,因为它们不能在序列化状态下充分表示

sql语句似乎在一行中查找三个值。我不是mysqli方面的专家,但这正是您想要做的:

public function get_something($id, $account_name){
  $sql = "SELECT one,two,three FROM table WHERE id = ? and account_name = ? ";
  $key = md5("SELECT one,two,three FROM table WHERE id = $id and account_name = $account_name ");

  $get_result = $this->Core->Core->Memcache->get($key);

  if($get_result){
    return $get_result;//#1 just return it, the format is an array like what is being built below
  }
  else{
   $stmt = $this->Core->Database->prepare($sql);
   $stmt->bind_param("is", $id, $account_name);
   $stmt->execute();
   $stmt->store_result();
   $stmt->bind_result($one, $two, $three);
   $stmt->fetch();
   //Below is how i set the data
   $this->Core->Template->set_data('one', $one);//#2 I don't know what this line does or is for, presumably for something else besides memcache stuff, maybe it acts like return
   //Set the Memcache
   $array=array();//#3
   $array[]=$one;
   $array[]=$two;
   $array[]=$three;

   $this->Core->Memcache->set($key, $array, TRUE, 20);
   //this is a function, do you want to return your values somewhere?
 }
请注意,#1您的问题的答案很简单,只需返回
$get_result
。它应该是一个具有三个值的数组#我不熟悉这一行,也不熟悉它的功能。这就是您将值“返回”到控制器的方式吗?如果是这样的话,你会想模仿我在
If
#3中放置
return
的那一行,这是你的问题。您无法将
$stmt
变量保存在memcache中,它是一个mysqli对象,而不是您想要的数据。您需要构建一个数组,然后保存该数组。这应该对你有好处

还有其他细微差别要做,您可以循环返回值。您应该检查mysql是否没有返回任何内容。但这是推动这一进程的基本出发点


让我知道这是否适合您。

为什么要保存准备好的语句而不是结果?后者应该可以工作。这是否意味着我设置了$stmt->fetch();to$results=$stmt->fetch();然后将memcache设置为$result而不是$stmt?因为如果我按照我刚才所说的做,就会发生此错误注意:memcache::get():无法取消序列化数据谢谢你的评论landon,我最终做了与你的答案非常相似的事情。。。我要做的是测试一下,看看它是否比我现有的更好。。。然后将此标记为答案。我的另一个担忧是,如果一个单独的函数删除或更新上述示例中的变量,我将如何更新或删除存储在Memcache中的变量?您必须确保更新/删除函数执行与Memcache记录类似的操作。您可以安全地将其删除,然后如果您对mysql查找的一次性成功感到满意,就可以使用它来填充它。因此,拥有一个非常有组织的键名结构非常重要,您需要准确地找到该特定键的名称,以便在其他地方编辑它时将其擦除