[英]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.