在PHP中缓存文件的最佳方式是什么?

4
我是一名有用的助手,以下是您需要翻译的内容:

我正在使用Smarty和我的PHP代码,并且希望缓存一些网站页面,因此我使用了以下代码:

// TOP of script
ob_start();   // start the output buffer
$cachefile ="cache/cachefile.html";
// normal PHP script 
$smarty->display('somefile.tpl.html') ;
$fp = fopen($cachefile, 'w'); // open the cache file for writing
fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
fclose($fp); // close the file
ob_end_flush(); // Send the output to the browser

但是当我在PHP文件末尾打印ob_get_contents()时,它是空的!实际上创建的缓存文件也是空的!那么当我使用Smarty时,如何在PHP中缓存文件?我知道我可以使用Smarty缓存,但由于某些原因它对我不起作用。
此外,请告诉我有关APC缓存的信息。如何使用它?在我的情况下值得使用吗?我认为它只用于缓存数据库查询,我阅读了PHP手册,但我什么也没懂 :)谢谢。
2个回答

1

我已经将文档中的一些代码(位于这里)混合在一起,以便更完整地展示smarty缓存的示例。此外,我不确定您在示例中使用了什么,但您应该使用smarty的方法来操作缓存。

    require('Smarty.class.php');
    $smarty = new Smarty;

    // 1 Means use the cache time defined in this file, 
    // 2 means use cache time defined in the cache itself
    $smarty->caching = 2; 

    // set the cache_lifetime for index.tpl to 5 minutes
    $smarty->cache_lifetime = 300;

    // Check if a cache exists for this file, if one doesn't exist assign variables etc
    if(!$smarty->is_cached('index.tpl')) {
        $contents = get_database_contents();
        $smarty->assign($contents);
    }

    // Display the output of index.tpl, will be from cache if one exists
    $smarty->display('index.tpl');

    // set the cache_lifetime for home.tpl to 1 hour
    $smarty->cache_lifetime = 3600;

    // Check if a cache exists for this file, if one doesn't exist assign variables etc
    if(!$smarty->is_cached('home.tpl')) {
        $contents = get_database_contents();
        $smarty->assign($contents);
    }

    // Display the output of index.tpl, will be from cache if one exists
    $smarty->display('home.tpl');

关于APC缓存,它的工作方式与Smarty相同。它们都将数据存储在文件中一段特定的时间。每次访问数据时,它都会检查缓存是否有效,如果有效,则返回缓存值。
但是,如果不使用Smarty,您可以这样使用APC:
此示例通过将DB查询结果存储在缓存中来进行演示,类似地,您可以修改此内容以代替存储整个页面输出,以便您不必频繁运行昂贵的PHP函数。
// A class to make APC management easier
class CacheManager  
{  
     public function get($key)  
     {  
          return apc_fetch($key);  
     }  

     public function store($key, $data, $ttl)  
     {  
          return apc_store($key, $data, $ttl);  
     }  

     public function delete($key)  
     {  
          return apc_delete($key);  
     }  
}  

结合一些逻辑,

function getNews()  
{  
     $query_string = 'SELECT * FROM news ORDER BY date_created DESC limit 5';  

     // see if this is cached first...  
     if($data = CacheManager::get(md5($query_string)))  
     {  
             // It was stored, return the value
          $result = $data;  
     }  
     else  
     {  
             // It wasn't stored, so run the query
          $result = mysql_query($query_string, $link);  
          $resultsArray = array();  

          while($line = mysql_fetch_object($result))  
          {  
               $resultsArray[] = $line;  
          }  

             // Save the result inside the cache for 3600 seconds
          CacheManager::set(md5($query_string), $resultsArray, 3600);  
     }  

     // Continue on with more functions if necessary 
}  

此示例稍作修改自此处

@Ian Elliott 是的,Smarty缓存是个好主意,但我不能使用它。因为我只有一个$ smarty->display('index.tpl');而其他页面(如news.tpl)都像这样进入我的index.tpl中心{include file=$page_center},然后在news.php文件中,我使用这行代码$smarty->assign('page_center','news.tpl');但当我启用缓存时,它仍会显示页面中心的默认内容而不是news.tpl,但当我关闭缓存时,它正常工作。 - mehdi
@mehdi,这听起来像是你需要使用自定义缓存ID——允许您缓存尽可能多的index.tpl版本。例如,在news.php中,您将调用$smarty->display('index.tpl','news|' . $article_id);对于帮助页面,您可以使用一个缓存ID为'help|' . $topic等。(使用管道字符来构造缓存ID允许您有选择地清除缓存,例如一次性清除所有新闻文章。) - searlea

1

你的意思是在调用ob_end_flush()之后再次调用ob_get_contents()吗?如果是这样,那么你写入文件的内容将会从PHP的内存中“删除”。

如果你仍然希望输出HTML,请先将ob_end_flush保存到一个变量中,然后将该变量传递给fwrite。你可以在页面下方稍后使用该变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接