Page level cache – PHP

Page level cache means that most frequent data will be stored in cache. So instead of requesting the same data from database, we will just use the cache to display that data. This will reduce the overhead on database by eliminating number of queries on SQL or any other database server you are using.

Cached data will be stored in files so it is advised not to store sensitive data, like passwords and credit cards, in cache. Although no one will know by which name you have saved the cache but in some circumstances it can be readable. So we must save that data in database which is not sensitive and we can use page level cache without any fear.

Once data is cached in a file then whenever a user requests that data it will not be fetched from database, instead it will be fetched from cache. This will decrease the page load time and hence will improve the performance of website. For example, if database query took 6 seconds to fetch the data then using this technique you can skip those 6 seconds wait.

You can also set the expiry time of cache. So if you have cached some data and now user is seeing the data from cached files. Now what if you made some changes in the database ? The user will still be seeing the old cached data ! That is where we need to set the expiry time of cache. A common practice is to expire the cache after 24 hours but you can customize it as per your needs.

Display page load time

First we need to find a way to check the actual page load time. So we can get the time when we were at 1st line of page and we can get the time when we were at last line of page. Then we will get difference between start and end time and we will know the number of seconds it take to load the page.

<?php

$start_time = time();

?>

<?php

$end_time = time();
echo "<h1>Difference: " . ($end_time - $start_time) . " seconds</h1>";

Create cache

We set the name of file where we want to store the cache. You can create a separate folder where all caches will be stored, most modern frameworks use this approach. We also will set the expiry time of cache so it will fetch fresh data after specified time period.

We will be using output buffering which is used to hold plain HTML before it is displayed in the browser. Whatever the content is displayed between ob_start() and ob_get_contents() will be save in the file.

Then we create a file using write mode and the content of file will be plain HTML that is rendered on the webpage. So if you open the cache file, you will see that it will have all plain HTML (same as you see when you do “view page source”). That is why it is recommended not to save sensitive data like passwords and credit cards in cache. Finally we can close the file object and when you run the script now, you will be able to see a new file created with HTML content.

<?php

$cache_file = "cache.php";
$cache_time = 60; // 1 minute

// Start output buffering
ob_start();

// run all DB queries here

$file = fopen($cache_file, "w");
fwrite($file, ob_get_contents());
fclose($file);

Read cache

We will read the cache only if the file exists and it has not been expired. When you run the script first time, then the file has not yet been created. So that means that you do not have any cache, after that we will check the expiry time of cache using file modified time.

Now if cache is found and it is not expired, then we will display the content from cache instead of requesting from database. To read the file we have 2 options, one is to use include or require function and second is to use readfile function. However, readfile is more secure than include so we will be using this.

You can use else condition if you want to perform some other action, that depends on your needs. But for simplicity we will stop the script after reading from cache using exit() function. Paste the following code after $cache_time variable and before ob_start() function.

<?php

$cache_time = 10; // seconds

if (file_exists($cache_file) && (filemtime($cache_file) + $cache_time > time()))
{
	readfile($cache_file);
	exit();
}

ob_start();

That’s it, now if you run the script you will see that when it loads from database, it takes more time than when it does from cache. The script is tested on multiple already created projects and by average it loads 5 seconds faster than using without cache.



Leave a Reply

Please disable your adblocker or whitelist this site!