Create a Fast Loading Dynamic PHP Site Using PHP Caching

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
https://donjajo.com/wp-content/uploads/2014/02/eggtimer.gif
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

1
public $cachefilename = 'cachefile.html';
  •   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
2
3
4
5
if($this->_checkTime(filemtime($this->cachefilename))==true)
{
//Seriously it has :/, Delete it :)
unlink($this->cachefilename);
}
THE PHP CODE

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
class Caching {
public $cachefilename = 'cachefile.html'; //Directory you want to save the cache file to

function __construct() {
//Lets check if the cache file exists
if(file_exists($this->cachefilename)) {

//Yea, the cache file of the page really exist, but lets check if it has reached its expiring time
if($this->_checkTime(filemtime($this->cachefilename))==true)
{
//Seriously it has :/, Delete it :)
unlink($this->cachefilename);
}

//Assuming its existing and it has not expired
else
{
//Load the cache and leave your db and other things to rest
include($this->cachefilename);
die();
}
}
}

function __deconstruct()
{
ob_end_flush();
}

function SaveCache() {
$file = fopen($this->cachefilename, 'w');
fwrite($file, ob_get_contents());
fclose($file);
}

function _checkTime($filetime,$time = 3) // $time value is in minutes, the default is 3 minutes
{
$t = time() - $filetime;
if($t/60>$time)
{
return true;
}
else
{
return false;
}
}
}
?>
HOW TO IMPLEMENT PHP CACHING CLASS INTO YOUR PAGE
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
//Always include the file which contains the Caching clss
include 'cache.php';

//Initiating the Caching Class, must be at the top of the code with the ob_start()
$cache = new Caching;
ob_start();
?>
<html>
<head>
</head>
<body>
HTML and PHP Code goes here...
</body>
</html>

<?php
//This must be at the end of the page to enable it get the content of the page after load
$cache->SaveCache();
?>
Posted in: