【问题标题】:Can i store mysql result in apc cache ?我可以将mysql结果存储在apc缓存中吗?
【发布时间】:2013-08-31 17:29:05
【问题描述】:

考虑这个示例代码:

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

有没有更好的缓存结果的方法?

【问题讨论】:

  • Helpers::execute_query 来自哪里?
  • 抱歉忽略。它是一个通用类,具有执行 mysql_query($query); 的 execute_query($query) 函数
  • 是的,我很担心。您不应该在新应用程序中使用mysql_query,其次,看起来$storyID 没有正确转义。这两件事都很糟糕。
  • 同意!非常感谢您的建议。 :) 关于如何以更好的方式实现缓存的任何想法?

标签: php mysql caching apc


【解决方案1】:

假设 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...
}

【讨论】:

  • 仪式!如果您第一次看到我们运行查询时,我们使用 while 循环将值存储在缓存中,并使用 foreach 来检索它。!有什么办法优化它?
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-24
  • 2012-03-11
  • 2017-01-01
  • 2020-04-30
  • 1970-01-01
  • 2015-07-20
相关资源
最近更新 更多