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: March 6th, 2017 by Ashvini Hyalij

Eluminous Technologies Pvt Ltd

Empowering Ideas Globally!
The best option for fulfilling all your offshore development needs. With 14+ years of experience, we understand it's not about the project / technology but about the Purpose. Understanding the purpose behind your software needs makes us unique from the competition.