PHP nasıl bir düz dosya dinamik bir sayfa önbelleğe alabilir?

3 Cevap

Ben veritabanı yükü azaltmak için bir önbellek sistemi rulo çalışıyorum.

I want to be able to compare timestamps of the last updated time the flat file was updated to make sure it's not too long ago that the file was last updated.

Here's the code:

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

Teşekkür ederim.

3 Cevap

Bir dosya için değişiklik zamanını almak için filemtime() kullanabilirsiniz.

Eğer aşağıdaki kodu kullanabilirsiniz geçerli dosyanın son değişiklik damgası almak istiyorsanız:

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

Neden php cacheing seçeneklerden birini kullanın out there? PHP Hızlandırıcı veya PHP: APC? Bunlar ne için istediğiniz bir rulo hazır çözüm sağlar.