我可以将mysql结果存储在apc缓存中吗?

时间:2013-08-28 14:51:10

标签: php mysql caching apc

请考虑以下示例代码:

    $storyID = 1;
    $detailsCache = 'details'.$storyID;
    if(!apc_exists($detailsCache)){
    $phases_details = <<<SQL
            SELECT stp.stp_id, 
               stp.stp_name, 
               stp.stp_position, 
               FROM story_phase stp
               WHERE stp.stp_stl_id = $storyID
    SQL;
   $resultdetails = Helpers::execute_query($phases_details,"Get phase details failed.");
**// i cant cache the result here like apc_store($detailsCache, $phases_details);**
}
$result_phases_details = apc_fetch($detailsCache);

while($row = mysql_fetch_assoc($result_phases_details)){
// some logic
}

缓存结果的更好方法是什么?

1 个答案:

答案 0 :(得分:0)

假设MySQL结果是一个资源(它似乎是基于您以后使用的mysql_fetch_assoc),那么无法通过APC存储。但是,如果首先将结果读出到PHP数据结构中,则可以将它们存储在APC中以供以后检索。

$resultdetails = Helpers::execute_query($phases_details,"Get phase details failed.");
$results = array();
while ($row = mysql_fetch_assoc($resultdetails)) {
  $results[] = $row;
}
apc_store($detailsCache, $results);

// Retrieval:
$results = apc_fetch($detailsCache);
foreach ($results as $row) {
  // some logic...
}