使用PHP缓存HTML输出

4
我想为我的网站上的php页面创建缓存。我找到了很多解决方案,但我想要一个脚本,可以从我的数据库生成一个HTML页面,例如:
我有一个类别页面,它从数据库中获取所有类别,因此脚本应该能够生成这样的HTML页面:my-categories.html。然后,如果我选择一个类别,我应该得到一个名为my-x-category.html的页面,以此类推,针对其他类别和子类别。
我可以看到一些网站的URL是这样的:wwww.the-web-site.com/the-page-ex.html
即使它们是动态的。
非常感谢您的帮助。

我认为你的问题需要一些澄清:你是想在内存中创建一个通常用于数据库的缓存,还是想知道如何根据数据库中的信息动态创建页面? - Tom
8个回答

12

检查 ob_start() 函数

ob_start();
echo 'some_output';
$content = ob_get_contents();
ob_end_clean();

echo 'Content generated :'.$content;

5
你可以使用URL重写获得这样的URL。例如:对于apache,请参见mod_rewrite。 http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html 实际上,您不需要创建文件。您可以创建文件,但这更加复杂,因为您需要决定何时在数据更改时更新它们。

但是,根据您的主机提供商的好坏,您可能会或可能不会访问URL重写...我知道我的1&1初学者套餐就没有这个功能:( - Adam Rezich
这是一篇很老的帖子,但我想澄清一下。自2007年我第一次注册1&1以来,1&1初学者套餐确实允许使用mod_rewrite。 - Alec Gorge

3
在我看来,这是最好的解决方案。我用它来缓存我的Android应用程序的JSON文件。它可以简单地在其他PHP文件中使用。 它可以将文件大小从约1MB优化到约163KB(gzip)

enter image description here

在您的目录中创建cache文件夹

然后创建cache_start.php文件并粘贴此代码

<?php
header("HTTP/1.1 200 OK");
//header("Content-Type: application/json"); 
header("Content-Encoding: gzip");

$cache_filename = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
$cache_filename = "./cache/".md5($cache_filename);
$cache_limit_in_mins = 60 * 60; // It's one hour


if (file_exists($cache_filename))
{
    $secs_in_min = 60;
    $diff_in_secs = (time() - ($secs_in_min * $cache_limit_in_mins)) - filemtime($cache_filename);
    if ( $diff_in_secs < 0 )
    {
        print file_get_contents($cache_filename);
        exit();
    }
}
ob_start("ob_gzhandler");
?>

创建cache_end.php并粘贴以下代码

<?php
$content = ob_get_contents();
ob_end_clean();
$file = fopen ( $cache_filename, 'w' );
fwrite ( $file, $content );
fclose ( $file );
echo gzencode($content);
?>

然后创建一个例如index.php的文件(您想要缓存的文件)

<?php
include "cache_start.php";
echo "Hello Compress Cache World!";
include "cache_end.php";
?>

如果您在代码中使用了setlocale,则创建缓存文件名时需要考虑这一点,否则您将为所有本地环境提供相同的版本。 - Codemonkey

3

手动缓存(创建HTML并将其保存到文件中)可能不是最高效的方法,但如果您想走这条路,我建议使用以下方法(摘自我编写的一个简单测试应用程序):

$cache_filename = basename($_SERVER['PHP_SELF']) . "?" . $_SERVER['QUERY_STRING'];
$cache_limit_in_mins = 60 * 32; // this forms 32hrs
// check if we have a cached file already
if ( file_exists($cache_filename) )
{
    $secs_in_min = 60;
    $diff_in_secs = (time() - ($secs_in_min * $cache_limit_in_mins)) - filemtime($cache_filename);
    // check if the cached file is older than our limit
    if ( $diff_in_secs < 0 )
    {
        // it isn't, so display it to the user and stop
        print file_get_contents($cache_filename);
        exit();
    }
}

// create an array to hold your HTML output, this is where you generate your HTML
$output = array();
$output[] = '<table>';
$output[] = '<tr>';
// etc

//  Save the output as manual cache
$file = fopen ( $cache_filename, 'w' );
fwrite ( $file, implode($output,'') );
fclose ( $file );

print implode($output,'');

如果您在代码中使用setlocale,则在创建缓存文件名时需要考虑这一点,否则您将为所有语言环境提供相同的版本。 - Codemonkey

1
我在Apache服务器上使用APC来进行所有的PHP缓存。

0

0

我从数据库负载、数据带宽和加载速度的角度考虑。我有一些页面在未来几年内不太可能更改,(我知道可以使用基于数据库的CMS系统很容易)。与美国不同,这里的带宽成本可能很高。对此有何看法,是创建HTML页面还是动态页面(PHP、ASP.NET)?无论如何,页面的链接都将存储在数据库中。


0

如果你不反对使用框架,可以尝试使用Zend Framework的Zend_Cache。它非常灵活,并且(与某些框架模块不同)易于实现。


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