0

考虑这个示例代码:

    $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
}

有更好的方法来缓存结果吗?

4

1 回答 1

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...
}
于 2013-08-28T14:59:01.957 回答