I have tried to implement php cache for my project however I could not able to find the solution to implement.The below functions used to represent the cache functions.
Code: Select all
var $cacheDir = '../cache/'; // The default directory where to store chached files
var $cacheFile = ''; // The content of the actual cached file
var $timeOut = 1000; // The time how long the cached file remains reusable
var $startTime = 0; // The startime to determine script execution time
var $cache = true;
----------------------------------------------------------------------------------------
function getMicroTime() {
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
----------------------------------------------------------------------------------------
function startCache(){
$this->startTime = $this->getMicroTime();
if ($this->status){
$this->cacheFile = $this->cacheDir.urlencode( $_SERVER['REQUEST_URI'] );
if ( (file_exists($this->cacheFile)) &&
((fileatime($this->cacheFile) + $this->timeOut) > time()) )
{
//Read file from the cache
$handle = fopen($this->cacheFile , "r");
$html = fread($handle,filesize($this->cacheFile));
fclose($handle);
// Display the content
echo $html;
//Display the rendering time
echo '<p>Total time: '
.round(($this->getMicroTime())-($this->startTime),4).'</p>';
//Exit from the code as we displayed cached data
exit();
}
else
{
// Set to save generated content into file
$this->caching = true;
// ob_start();
}
}
}
--------------------------------------------------------------------------------------------------------------
function endCache(){
if ($this->status){
if ( $this->caching )
{
//You were caching the contents so display them, and write the cache file
$html = ob_get_clean();
echo $html;
$handle = fopen($this->cacheFile, 'w' );
fwrite ($handle, $html );
fclose ($handle);
//Display the rendering time
echo '<p>Total time: '.round(($this->getMicroTime()-$this->startTime),4).'</p>';
}
}
} Thanks and Regards,
Venkatesan.R