2011-10-16 28 views
2

简单的PHP缓存(与ob_start和文件),我需要部分(更多 - 或等于3每页)不缓存(.PHP动态内容或.PHP文件上下文 - 基于用户)。简单的PHP缓存超过3部分

+-----------------+ 
| CACHING CONTENT | 
|     | 
+-----------------+ 
| NO CACHING | 
+-----------------+ 
| CACHING CONTENT | 
+-----------------+ 
| NO CACHING | 
+-----------------+ 
|     | 
| CACHING CONTENT | 
+-----------------+ 

在“no caching”部分我想包含动态内容。我可以缓存三个cached.html文件(选项1),但我更喜欢每个缓存页面只有一个文件(而不是3页,选项2)。什么是缓存的最佳选择?在几个文件中

  1. 缓存(head_tag.html,body_part1.html,body_part2.html,body_part3.html ...)和中间动态内容(files.php)。
  2. 缓存独特的文件,用标签与动态内容替换(与...如何?)
  3. 其他

注意:请,没有第三个系统解决方案(memcached的,APC ...) 。我需要从基于PHP的选项。

回答

2

您可以使用占位符页面的非缓存部分,缓存整个页面。例如,整个缓存页面可能看起来像:

<html> 
... (static content) 
#DYNAMIC-CONTENT-NAME# 
... (static content) 
#SECOND-DYNAMIC-CONTENT-PLACEHOLDER# 
... (static content) 
</html> 

然后在PHP中,您只需将获得此缓存网页,并与动态内容替换所有占位符。

// obtain the cached page from storage 
$cached_page = get_cached_page(); 

// generate the HTML for the dynamic content 
$dynamic_content = get_dynamic_content(); 

// replace the placeholders with the actual dynamic content 
$page = str_replace('#DYNAMIC-CONTENT-NAME#', $dynamic_content, $cached_page); 

// display the resulting page 
echo $page; 

这样一来,只要你喜欢,动态内容一样多,只要你喜欢,那么你只需用实际内容替换他们,你可以放置多个指定的占位符。

+0

是啊!那是我的想法。有没有最佳实践指南? – Manz

+0

@Manz,好吧,这很简单,我不确定最佳实践指南是什么意思......你有一个HTML页面(如果你只有静态内容,你甚至不需要制作.php)占位符。你缓存这个页面,然后当你需要显示它时,你从缓存中获取它,并用一个简单的'str_replace()'替换占位符... – rid

0

有两种方式与直PHP来做到这一点

页眉计算策略

$cachetime = 60 * 60 * 24 * 7; // 1 Week 
header(‘Expires: ‘.gmdate(‘D, d M Y H:i:s’, time()+$expires).’GMT’); 

或者通过缓存完整的文件(包括从动态内容/内容)在你的文件系统(可用于网站的缓存部分)

<?php 
    $cachefile = "cache/".$reqfilename.".html"; #change $reqfilename to $_SERVER['PHP_SELF'] if you are using in headers, footers, menus files 
    $cachetime = 60 * 60 * 24 * 7; // 1 Week 
    // Serve from the cache if it is younger than $cachetime 
    if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) { 
    include($cachefile); 
    exit; 
    } 
    ob_start(); // start the output buffer 
?> 
.. Your usual PHP script and HTML here ... 
<?php 
    // open the cache file for writing 
    $fp = fopen($cachefile, 'w'); 

    // save the contents of output buffer to the file 
    fwrite($fp, ob_get_contents()); 
    // close the file 
    fclose($fp); 
    // Send the output to the browser 
    ob_end_flush(); 
?> 

什么你也可以做的是通过使用头或更新缓存信息您的htaccess缓存用户计算机上的文件。 htaccess的实现可能会有所不同,具体取决于托管服务器上安装的模块。我使用:

# Add Expiration 
ExpiresActive On 
ExpiresDefault "access plus 1 week" 
ExpiresByType text/html "access plus 1 day" 
ExpiresByType text/php "access plus 1 day" 
ExpiresByType image/gif "access plus 1 week" 
ExpiresByType image/jpeg "access plus 1 week" 
ExpiresByType image/png "access plus 1 week" 
ExpiresByType text/css "access plus 1 week" 
ExpiresByType text/javascript "access plus 1 week" 
ExpiresByType application/x-javascript "access plus 1 week" 
ExpiresByType image/x-icon "access plus 1 week" 
ExpiresByType image/ico "access plus 1 week" 
ExpiresByType text/xml "access plus 1 day" 
+0

我想要中间PHP内容的3部分缓存。我认为最好的选择:缓存到独特的文件...但如何?很好的答案,但也许我不会在我的问题中解释我。尝试编辑问题澄清。 – Manz

+0

php缓存可用于缓存非动态网站的部分内容。您可以为动态内容实施修改时间的数据库,以便在内容更新后第一次调用页面时更新数据库。 –