To implement caching in core php follow the below steps : -
Step 1 - Firstly create a folder having write permission where we store the output of the page.
Step 2 - At the start of file, please check whether we have already stored any output of the file or there is no output stored yet.
Step 3 - In case If you have already stored the output in a cache, then first get the contents from the cache before executing the code.
Step 4 - In case it's Not, then first prevent the php for sending the output to the browser (use ob_start php) and store all tthe output in to a cache folder.
Step 5 - Now create a page in to our application from where we can delete all the files,and then stored in cache folder (note that its for when website updates).
Example:-
$fileName="xxxx.html";
$cacheFolder="/cache/";//must be writeable
//check if file in cache
if(file_exists($cacheFolder.$fileName,'r'){
$output = file_get_contents($cacheFolder.$fileName);
}else{
ob_start(); //stop sending output to the browser
//do work
//do work
//do work
$output = ob_get_contents();
ob_end_flush();
$fp=fopen($cacheFolder.$fileName,'w');
fwrite($fp, $output);
fclose($fp);
}
echo $output;
0 Comment(s)