简体   繁体   English

Memcache和Mysqli准备的语句问题

[英]Memcache and Mysqli Prepared Statement Issue

This has been driving me crazy, the issue is I cannot work out How i can get and set the cached data to be displayed within my view. 这使我发疯,问题是我无法解决如何获取和设置要在视图中显示的缓存数据。

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

So my question is how can I get and set the data from a prepared statement fetch within memcache? 所以我的问题是如何从内存缓存中的预备语句获取中获取并设置数据?

Memcache is a key/value storage system with both the key and the value needing to be serialized. Memcache是​​一个键/值存储系统,其中键和值都需要序列化。 From the php.net page: php.net页面:

Remember that resource variables (ie file and connection descriptors) cannot be stored in the cache, because they cannot be adequately represented in serialized state. 请记住,资源变量(即文件和连接描述符)不能存储在缓存中,因为它们不能在序列化状态下充分表示。

It appears your sql statement is looking for three values in a single row. 看来您的sql语句正在单行中查找三个值。 I'm no expert on mysqli, but this is kind of what you want to do: 我不是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?
 }

A few notes, #1 the answer to your question is simple, just return $get_result . 一些注意事项,#1您问题的答案很简单,只需返回$get_result It should be an array with three values. 它应该是具有三个值的数组。 #2 I'm not familiar with this line, nor what it does. #2我不熟悉这条线,也不知道它的作用。 Is this how your "return" the values to your controller? 这是您将值“返回”到控制器的方式吗? If so, you'll want to mimick that line where I put the return inside the if #3 This is your problem. 如果是这样,您将想模仿我将return放在if #3中的那行,这是您的问题。 You can't save the $stmt variable in memcache, it's a mysqli object, not the data you want. 您不能将$stmt变量保存在memcache中,这是一个mysqli对象,而不是所需的数据。 You need to build an array and then save that array. 您需要构建一个数组,然后保存该数组。 And that should do it for you. 那应该为您做。

There are other nuances to do, you can loop on the returned values. 还有其他细微差别,您可以循环返回的值。 You should check for mysql not returning anything. 您应该检查mysql是否未返回任何内容。 But this is the basic starting point to get this going. 但这是进行此操作的基本起点。

Let me know if this works for you. 让我知道这是否适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM