Hi,
I've created a script to understand the logic of caching. Do you think my example below makes sense? I am asking because I've never used caching before.
1. Check if HTML version of current testfile.php exist in cache folder as testfile.html
a. If YES then compare content of testpage.php with testpage.html
i. If content is same then print from HTML. DONE!
ii.If not then print content of testpage.php and create testpage.html to be used in future. DONE!
b. If NO then print content of testpage.php and create testpage.html to be used in future
Note: I don't think this example is compatible with dynamicaly generated webpages. If I am correct then could you please help me to enhance this example?
Thanks
testpage.php
<?php
//**********************************************************************
//** CACHE CLASS *******************************************************
//**********************************************************************
class CacheBase{
public function createCacheFile($pageNameIN, $contentIN){
// Create copy of php file and save as html in cache/ folder
$newFile = fopen("cache/".$pageNameIN.".html", "w");
fwrite($newFile, $contentIN);
fclose($newFile);
}
public function checkContentBased($pageNameIN, $contentIN){
// Get the content of existing html file
$existingContent = file_get_contents("cache/".$pageNameIN.".html");
// Return relevant values based on result.
// True = Overwrite existing file with new content
// $existingContent = Don't overwrite existing file just print
if($contentIN == $existingContent){
return $existingContent;
} else {
return true;
}
}
}
//**********************************************************************
//** END ***************************************************************
//**********************************************************************
// Start output buffering to catch all content from
ob_start();
$body = "<html>\n";
$body.= "<head>\n";
$body.= "<title>Static caching</title>\n";
$body.= "</head>\n";
$body.= "<body>\n";
$body.= "<p>This testpage.php/html cache file generated automaticaly</p>\n";
$body.= "</body>\n";
$body.= "</html>\n";
echo $body;
// Save all the content from webpage into a valiable
$content = ob_get_contents();
// Clear the buffer not to submit to the client
ob_end_clean();
//**********************************************************************
//** CACHE CONTROL *****************************************************
//**********************************************************************
// Initiate Cache class
$objCache = new CacheBase();
// Get the name of the file from URL into a variable
$pageName = testfile;
// If the copy of requested file doesn't exist in cache folder as html
// then create it and print it otherwise continue further checks
if(! file_exists("cache/".$pageName.".html")){
// Print PHP content for the first time as it will be the same
// as new HTML copy just will be created below
echo $content;
$objCache->createCacheFile($pageName, $content);
} else {
// $overWrite = either true or copy of HTML file from cache
$overWrite = $objCache->checkContentBased($pageName, $content);
// If new content differs from existing one then overwrite html file
// stored in cache folder otherwise use old HTML copy from cache
if($overWrite === true){
// Print PHP content for the first time as it will be the same
// as new HTML copy just will be created below
echo $content;
$objCache->createCacheFile($pageName, $content);
} else {
// Old cache HTML file is being used
echo $overWrite;
}
}
//**********************************************************************
//** END ***************************************************************
//**********************************************************************
?>