【问题标题】:Simple PHP caching with more than 3 parts包含 3 个以上部分的简单 PHP 缓存
【发布时间】:2013-10-27 13:52:16
【问题描述】:

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

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

在“无缓存”部分中,我希望包含动态内容。我可以缓存三个 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 的选项。

【问题讨论】:

    标签: php caching


    【解决方案1】:

    您可以为页面的非缓存部分使用占位符,并缓存整个页面。例如,整个缓存页面可能如下所示:

    <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;
    

    这样,您可以为任意多的动态内容放置任意数量的命名占位符,然后您只需将它们替换为实际内容。

    【讨论】:

    • 是的!那是我的想法。有这方面的最佳实践指南吗?
    • @Manz,好吧,这很简单,我不确定您所说的最佳实践指南是什么意思...您有一个 HTML 页面(如果你只有静态内容)和占位符。你缓存这个页面,然后当你需要显示它时,你从缓存中获取它并将占位符替换为一个简单的str_replace()...
    【解决方案2】:

    直接使用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"
    

    【讨论】:

    • 我想要包含中间 PHP 内容的 3 部分缓存。我认为最好的选择:缓存到唯一文件......但是如何?很好的答案,但也许我没有在我的问题中解释我。尝试编辑问题以澄清。
    • php 缓存可用于缓存网站的非动态部分。您可以为动态内容实现一个修改时间的数据库,以便在内容更新后第一次调用页面时更新它。
    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多