Krillz.com

A coder’s steps revealed


Search




Well written and optimized code is something all programmers at one point have to start thinking about while coding.
Today this is more important than ever before, not to mentions the need to create efficient web applications that don’t require a lot of computer resources to function.
Not thinking about efficiency today is impossible and shows the lack of professionalism in what you do.
With that said I’ll be covering one method that you can use to deal with this but also hopefully get you thinking about these matters

What you should know

  • PHP5
  • OOP and basic HTML

Introduction

With the constantly growing knowledge we have started to think more about how to code something, but code in the most optimal way.
We want to have the assurance that it will work just fine and fast even under a huge load and it here’s where the buffering comes into the picture.
Especially files with content that doesn’t have to be regenerated with every request.
I’ll be covering two examples where one also covers exceptions.

Buffering a dynamic www page

Imagine that we have a web service developed in PHP and MySQL.
Each time all of the subpages are generated dynamically which means that the server has parse the PHP code, connect with database, generate the page and finally show it.
Not a big problem right?
But what happens if the server had to deal with thousands of these request at the same time?
Well most likely the rendering or loading time of that page will take much longer time than if the server only had ten requests, which could lead to a potential visitor to change his mind about visiting the page and instead go somewhere else.

Now it’s here where the buffering comes to the rescue as with the help of buffering we can save an already generated page into a file which will act as out buffer.
Depending on our needs we can choose to refresh the buffer after some changes have been made to the page or after a certain amount of time have passed.

Using the buffer you can really experience the difference under that hectic period when the server load is huge, compared to the non buffered page it will be loaded a lot faster.
This technique is also very useful when we are collecting data from a database on another server as the data can easily be saved in a XML file, which will save time in terms of connecting, transferring the data and to format the data as we want it.
Instead we can save that as we want it in our XML file instead and have it displayed fast and easy.

In my example I’ll be using a very simple www page, which I’ll be buffering.
Simple as in no graphics or database connections, I want to keep this nice and simple so that even the new PHP programmers can understand.
A more advanced and detailed article is possible if you readers want one.

The page will only consist of one dynamic element which will display the time in terms of hours and minutes.

<HTML>
<BODY>
   The present time is: &nbsp;
   <?php echo date(“H:i”); ?>
</BODY>
</HTML>

So this straight forward page will show the visitors the time they loaded the page.
The content or more exactly the time will each time be generated by PHP and it doesn’t take a genius to realize that the page will display the exact same thing for a whole minute or 60 seconds.
So is it really necessary to have the page regenerated during that interval if the page obviously doesn’t change its content?

It would be a lot better if the page remained static for that time instead.
So we will be saving the generated page into a file instead making it static and changing it every 60 seconds.

Now in my example the time accessing the file may be greater than generating the page, with this in mind I still hope you’ll see my intensions with this easy example and be able to implement this method in bigger projects where this approach will be noticeable and appreciated.
Now take a look on the following bit of code

/*
Page with implemented buffering - generated every 60 seconds
Rest of the time it’s static
*/

<?php
$file_name = ‘temp.cache’;
if (file_exists($file_name) && (filetime($file_name) + 60)>time()){
  include($file_name);
  echo ‘<br />The page was loaded’;
 
}

$file_tmp = $file_name.‘.’.getmypid();
$file_handle = fopen($file_tmp,“w”);
ob_start();
?>

<HTML>
<BODY>
   The present time is: &nbsp;
   <?php echo date(“H:i”); ?>
</BODY>
</HTML>

<?php

if($file_handle) {
 
  $page_content = ob_get_contents();
  fwrite($file_handle, $page_content);
  fclose($file_handle);
 
  if (file_exists($file_name)) { unlink($file_name); }
  rename ($file_tmp,$file_name);
 
 }
 
 ob_end_flush();
 ?>

As you can see our script has grown a little bit and it’s time to explain what has happen.
In the first part of the script we declare the name of our file.
Next we check if our file has been created and if it has been opened in the time frame of more than 60 seconds otherwise we have to update the content.
If both conditions are true our page is shown from the saved file along with the ‘page was loaded’ text. That extra line is just there to make the example more clear for you.
If the conditions end up being false we declare the name of our temporary file where the buffered page will be stored in the meantime before it is saved in the tmp.cache file.
The temporary file get’s a different name each time thanks to the added name of the process identifier which is unique in any given moment.

This isn’t that relevant in our case but more so if you had changes made by the users as an argument to update the buffered site.

The next step is; we open our file to be able to write, if the file doesn’t exist it will be created which will be the case for us, and then by using ob_start() we begin the buffering of the page which was supposed to be outputted.

All the output that was supposed to be shown will now instead be stored in the buffer.
After the page is loaded to the buffer we want to save it or more precisely write it into a file so we check if we still have access to the temporary file.

If the check returns true all the content in the buffer is copied to $page_content using the function ob_get_content and then saved into the file temp.cahce.

End of part one
buffering-www-applications-with-php-part-2

Share with the world: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Slashdot
  • del.icio.us
  • Digg
  • Technorati
  • Reddit
  • StumbleUpon
  • YahooMyWeb
  • DZone
  • MisterWong

One Response to “Buffering www applications with PHP”

  1. Programming Tutorials…

    I couldn’t understand some parts of this article, but it sounds interesting…

    Programming Tutorials

Leave a Reply