如何在PHP中将动态页面缓存到平面文件中?

如何在PHP中将动态页面缓存到平面文件中?,php,Php,我正在尝试使用缓存系统来减少数据库的负载 我希望能够比较平面文件上次更新的时间戳 以确保该文件最近一次更新是在不久之前 代码如下: $cache_file = $_GET[ 'page_id' ] . '.html'; function cache() { // Here i want to get the timestamp of the file that was previously cached, // if it exists. // If the fil

我正在尝试使用缓存系统来减少数据库的负载

我希望能够比较平面文件上次更新的时间戳 以确保该文件最近一次更新是在不久之前

代码如下:
$cache_file = $_GET[ 'page_id' ] . '.html';

function cache() {

    // Here i want to get the timestamp of the file that was previously cached, 
    // if it exists.
    // If the file doesn't exist, create it.
    // If it does exist, check last modified time, if it's too long ago, then overwrite
    // the file.


    $ob = ob_get_contents();

    file_put_contents( $cache_file, $ob );

}

function loadFromCache( $page_id ) {
    $file_name = $page_id . '.html';
    if( ! file_exists( $file_name  ) ) {
         return false;
    }
    readfile( $file_name );
    return true;
}

谢谢。

您可以使用获取文件的修改时间。

您可以使用获取文件的修改时间。

如果您想获取当前文件上次修改的时间戳,可以使用以下代码:

<?php
    $stat = (stat(__FILE__));
    echo 'Document last updated: ', date('M d Y', $stat['mtime']);
?>

如果要获取当前文件上次修改的时间戳,可以使用以下代码:

<?php
    $stat = (stat(__FILE__));
    echo 'Document last updated: ', date('M d Y', $stat['mtime']);
?>

为什么不使用php缓存选项之一?PHP加速器还是PHP:APC?它们为您提供了一个现成的解决方案。

为什么不使用php缓存选项之一?PHP加速器还是PHP:APC?这些为您提供了现成的解决方案。

我不想要当前文件。我想要缓存文件的修改时间。我不想要当前文件。我想要缓存文件的修改时间。如果您只是输出文件,而不是echo file\u get\u contents$file\u name;只读取文件$file\u name;应该会更快;。这会将文件内容输出到浏览器,而无需首先将整个文件加载到内存中。如果您只是输出文件,请不要使用echo file\u get\u contents$file\u name;只读取文件$file\u name;应该会更快;。将文件内容输出到浏览器,而不首先将整个文件加载到内存中。APC不会缓存输出,而是缓存字节码,然后执行。APC的默认行为不会减少数据库负载,这正是他所要求的。您可以自己使用APC的用户密钥存储来存储输出,但坦率地说,这并不是APC的强项。APC不缓存输出,而是缓存字节码,然后执行字节码。APC的默认行为不会减少数据库负载,这正是他所要求的。您可以自己使用APC的用户密钥存储来存储输出,但坦率地说,这并不是APC的优势所在。