Easiest PHP Page Caching Technique To Speed Up Web Page Load Time


Which one of us don’t want to have a huge traffic on our websites? We would always be pleased with the growing visitors & ultimately our businesses. But with this, what may happen? With this extension, web pages may load slowly which could affect the engagement & thus ROI. Being a back end developer, I would like to share a simple page caching technique to speed up the page loading time.

As we know, each client-server request consists of many loops, calculations, database queries etc. All these adds processing time, which increases page loading time. The simplest way to avoid all these and speed up the site is to create cache files and store them in a separate directory. Later on, these cached files are served as fast loading static pages instead of generating dynamic pages. You can set how often the page cache is flushed out depending on how often your site’s content get changed.Combining PHP Output Buffer and Filesystem Functions, we can generate magnificent caching system.

PHP Output buffer:
The output is not sent to the browser in pieces but the whole HTML page as one variable. It decreases the amount of time it takes to download, hence improves the performance. Take a look at the code below:

Call ob_start() on the top of the code, it turns on output buffering, that means anything after this function will be stored in the buffer, instead of sent it to the browser. ob_get_contents() is used to retrieve the content of the buffer. ob_end_flush() should be called at the end of the code to send the output to the browser and turn buffering off.

PHP Filesystem
Use PHP file system, it is a part of the PHP core which allows us to read and write the file. Have a look at the following code.

The first line of the code fopen() opens the file for writing, the mode ‘W’’ places the file pointer at the beginning of the file and if the file does not exist, it attempts to create one. Second line fwrite() writes the string to the opened file, and finally fclose() closes the successfully opened file at the beginning of the code.

PHP caching

The cycle starts when a user requests the content, the system just checks whether the cache copy for the currently requested page exists, if it doesn’t exist the system generate a new page, create cache copy and then output the result. If the cache already exists, we just have to fetch the file and send it to the user browser.
Take a look at the Full PHP cache code below,

Conclusion:
I hope this article will help back end developer to create cache system . Avoid caching in a constantly updating pages such as members area (after user logs in), search pages etc. Needless to say, grow your audience without any limitation & make it work for you.

Easiest PHP Page Caching Technique To Speed Up Web Page Load Time was last modified: November 13th, 2017 by Ashvini Hyalij

One thought on “Easiest PHP Page Caching Technique To Speed Up Web Page Load Time

Leave a Reply

Your email address will not be published. Required fields are marked *