Query not your database too much and leave it to rest, which will absolutely make your page load as fast as a local html file in your PC
WHAT IS CACHING?
This is the act of storing a data for future retrieval in a faster way.
WHERE IS CACHING NEEDED?
Well, I should say when you are having a static website; there is no need to cache because it does not communicate with database and browsers loads html web pages so fast.
Dynamic site is where Caching is needed in other to load the page so fast and not communicating with your database frequently.
HOW DOES PHP CACHING WORKS?
Below is the coding but let me explain a little bit about it here. There is no big deal about it;
- On page load, save the HTML source code of the page in a file as a .html file extension in a specific directory which you will specify here
|
|
public $cachefilename = 'cachefile.html';<br /> |
|
- On next page load, check if the cache file of the current page exists
- If it exists, check if the expiration (which I set 3 minutes as default) has reached because a cache file cannot be forever, you need to delete it and create another so that new contents from the database can be updated in the page.
- Now if the expiration time has reached, delete the file and create new one else load it and stop other codes from executing
|
1<br />2<br />3<br />4<br />5 |
|
|
if($this->_checkTime(filemtime($this->cachefilename))==true)<br /> {<br /> //Seriously it has :/, Delete it :)<br /> unlink($this->cachefilename); <br /> }<br /> |
|
|
1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50 |
|
|
<span style="color: #557799;"><?php</span><br /><span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Caching</span> {<br /> <span style="color: #008800; font-weight: bold;">public</span> <span style="color: #996633;">$cachefilename</span> <span style="color: #333333;">=</span> <span style="background-color: #fff0f0;">'cachefile.html'</span>; <span style="color: #888888;">//Directory you want to save the cache file to </span><br /><br /> <span style="color: #008800; font-weight: bold;">function</span> <span style="color: #0066bb; font-weight: bold;">__construct</span>() {<br /> <span style="color: #888888;">//Lets check if the cache file exists</span><br /> <span style="color: #008800; font-weight: bold;">if</span>(<span style="color: #007020;">file_exists</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">cachefilename</span>)) {<br /> <br /> <span style="color: #888888;">//Yea, the cache file of the page really exist, but lets check if it has reached its expiring time</span><br /> <span style="color: #008800; font-weight: bold;">if</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">_checkTime</span>(<span style="color: #007020;">filemtime</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">cachefilename</span>))<span style="color: #333333;">==</span><span style="color: #008800; font-weight: bold;">true</span>)<br /> {<br /> <span style="color: #888888;">//Seriously it has :/, Delete it :)</span><br /> <span style="color: #007020;">unlink</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">cachefilename</span>); <br /> }<br /> <br /> <span style="color: #888888;">//Assuming its existing and it has not expired</span><br /> <span style="color: #008800; font-weight: bold;">else</span> <br /> {<br /> <span style="color: #888888;">//Load the cache and leave your db and other things to rest</span><br /> <span style="color: #008800; font-weight: bold;">include</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">cachefilename</span>);<br /> <span style="color: #008800; font-weight: bold;">die</span>();<br /> }<br /> }<br /> }<br /><br /> <span style="color: #008800; font-weight: bold;">function</span> <span style="color: #0066bb; font-weight: bold;">__deconstruct</span>() <br /> {<br /> <span style="color: #007020;">ob_end_flush</span>();<br /> }<br /><br /> <span style="color: #008800; font-weight: bold;">function</span> <span style="color: #0066bb; font-weight: bold;">SaveCache</span>() {<br /> <span style="color: #996633;">$file</span> <span style="color: #333333;">=</span> <span style="color: #007020;">fopen</span>(<span style="color: #996633;">$this</span><span style="color: #333333;">-></span><span style="color: #0000cc;">cachefilename</span>, <span style="background-color: #fff0f0;">'w'</span>);<br /> <span style="color: #007020;">fwrite</span>(<span style="color: #996633;">$file</span>, <span style="color: #007020;">ob_get_contents</span>());<br /> <span style="color: #007020;">fclose</span>(<span style="color: #996633;">$file</span>);<br /> }<br /> <br /> <span style="color: #008800; font-weight: bold;">function</span> <span style="color: #0066bb; font-weight: bold;">_checkTime</span>(<span style="color: #996633;">$filetime</span>,<span style="color: #996633;">$time</span> <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">3</span>) <span style="color: #888888;">// $time value is in minutes, the default is 3 minutes</span><br /> {<br /> <span style="color: #996633;">$t</span> <span style="color: #333333;">=</span> <span style="color: #007020;">time</span>() <span style="color: #333333;">-</span> <span style="color: #996633;">$filetime</span>;<br /> <span style="color: #008800; font-weight: bold;">if</span>(<span style="color: #996633;">$t</span><span style="color: #333333;">/</span><span style="color: #0000dd; font-weight: bold;">60</span><span style="color: #333333;">></span><span style="color: #996633;">$time</span>)<br /> {<br /> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">true</span>;<br /> }<br /> <span style="color: #008800; font-weight: bold;">else</span><br /> {<br /> <span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">false</span>;<br /> }<br /> }<br />}<br /><span style="color: #557799;">?></span><br /> |
|
HOW TO IMPLEMENT PHP CACHING CLASS INTO YOUR PAGE
|
1<br /> 2<br /> 3<br /> 4<br /> 5<br /> 6<br /> 7<br /> 8<br /> 9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20 |
|
|
<span style="color: #557799;"><?php</span><br /><span style="color: #888888;">//Always include the file which contains the Caching clss</span><br /><span style="color: #008800; font-weight: bold;">include</span> <span style="background-color: #fff0f0;">'cache.php'</span>;<br /><br /><span style="color: #888888;">//Initiating the Caching Class, must be at the top of the code with the ob_start()</span><br /><span style="color: #996633;">$cache</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Caching;<br /><span style="color: #007020;">ob_start</span>();<br /><span style="color: #557799;">?></span><br /><html><br /><head><br /></head><br /><body><br />HTML and PHP Code goes here...<br /></body><br /></html><br /><br /><span style="color: #557799;"><?php</span><br /><span style="color: #888888;">//This must be at the end of the page to enable it get the content of the page after load</span><br /><span style="color: #996633;">$cache</span><span style="color: #333333;">-></span><span style="color: #0000cc;">SaveCache</span>();<br /><span style="color: #557799;">?></span><br /> |
|