I am trying to roll a caching system to reduce the load on the database.
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.
$o开发者_开发问答b = 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;
}
Thank you.
You can use filemtime()
to get the modification time for a file.
If you want to get timestamp of the last modification of the current file you can use code below:
<?php
$stat = (stat(__FILE__));
echo 'Document last updated: ', date('M d Y', $stat['mtime']);
?>
Why not use one of the php cacheing options out there? PHP Accelerator or PHP:APC? These provide a ready rolled solution for what you want.
精彩评论