考虑以下示例代码:
$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
}
还有什么更好的方法来缓存结果?
最佳答案
假设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...
}