[英]Memcache and Mysqli Prepared Statement Issue
这使我发疯,问题是我无法解决如何获取和设置要在视图中显示的缓存数据。
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是一个键/值存储系统,其中键和值都需要序列化。 从php.net页面:
请记住,资源变量(即文件和连接描述符)不能存储在缓存中,因为它们不能在序列化状态下充分表示。
看来您的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
。 它应该是具有三个值的数组。 #2我不熟悉这条线,也不知道它的作用。 这是您将值“返回”到控制器的方式吗? 如果是这样,您将想模仿我将return
放在if
#3中的那行,这是您的问题。 您不能将$stmt
变量保存在memcache中,这是一个mysqli对象,而不是所需的数据。 您需要构建一个数组,然后保存该数组。 那应该为您做。
还有其他细微差别,您可以循环返回的值。 您应该检查mysql是否未返回任何内容。 但这是进行此操作的基本起点。
让我知道这是否适合您。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.