/    Sign up×
Community /Pin to ProfileBookmark

Buffered vs cached?

I was looking into some ways to improve my page load speeds. I came across two methods, I could use output buffers or I could use the Alternative PHP Cache, or one of the many other accelerators out there.

I was wondering if anyone could tell me the difference between buffering and caching, and which would be optimal for what I’m trying to do? I don’t have experience in either, so I would be learning something new either way.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiMay 27.2009 — Buffering just stores output in an internal buffer rather than sending it directly to the client. Caching usually involves storing some or all of the page as a static file for a certain period of time and serving it directly rather than regenerating the output at every request.

I can see no reason why output buffering should make any real difference to the speed a page loads. It is still doing the same processing, and the same amount of data is sent to the browser. Speed is not really anything to do with the purpose of buffering.

Caching (especially full page caching) can make massive differences, especially if your script is quite heavy on processing. It is also easy to set up. However, when I say "massive differences", understand that massive differences in PHP processing time is still (usually) in the realm of fractions of a second so in reality will not make a real difference.

The server side processing is (usually) small fry compared to the network overhead, ie the time it takes to actually send the page to the browser. There are a few ways to improve this (not PHP related).

Firstly, ensure you set an expires header on static content. When a request is made to a web server, a check is first made by the browser to see if it has an item in its cache, and if so if that item has expired. If it has not expired, it will not bother requesting it again. By default, the expires date is pretty short. Adding one far into the future for content which doesn't change often will reduce the number of HTTP requests (quite dramatically in most cases). You can do this in an .htaccess file for the Apache server:

<i>
</i>&lt;FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"&gt;
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
&lt;/FilesMatch&gt;


You can also reduce the number of requests by using techniques such as CSS sprites for images, combining JS and CSS files etc.

Another useful thing to know is that modern browsers will download more than one item at a time, UNLESS it is downloading a script. When downloading a script, no other consecutive downloads take place. If you have 6 javascripts at the top of your file, you have to wait for all 6 to download one after the other before anything below them can be downloaded. In alot of cases your scripts wont be doing anything until the page is loaded anyway, so placing them just before the </body> tag can speed things up considerably.

Another thing which helps alot if gzipping output on the server side before sending it to the client.

So the short answer is, page caching will certainly speed things up, but if you really need to increase load times you need to look beyond your PHP code.
Copy linkTweet thisAlerts:
@bejitto101authorMay 27.2009 — Thanks for the information and help! I'll definitely try to implement these. Couple questions on a couple issues though.

What do you mean by using CSS sprites for images?

How would I go about gzipping output, and what exactly would I gzip?

What is the purpose of buffering then?

Sorry for all the questions, two years into web dev and I'm still a noob.
Copy linkTweet thisAlerts:
@Mr__E__CrypticMay 27.2009 — What do you mean by using CSS sprites for images?[/QUOTE]
http://css-tricks.com/css-sprites/

A general collection of tricks from my local, friendly search engine (strangely, not Yahoo):

http://developer.yahoo.com/performance/rules.html
Copy linkTweet thisAlerts:
@MindzaiMay 28.2009 — The Yahoo link posted above answers your first questions.


What is the purpose of buffering then?[/QUOTE]


Like most things in programming, it's purposes are many. However as an example of something you might want to use it for, imagine you have a CMS which stores HTML/PHP content in a database. You can retrieve this content from the database as a string and evaluate it to build the page content, but you would want to store the evaluated output in a variable rather than just evaluating it directly (which would interfere with your CMS's code). So you can use output buffering to capture the content's output in a string variable (excude the messed up formatting, the forum's editor gets confused easily:

[code=php]
$someContent = '<div>Here is some PHP: <?php echo "Hello, I come from PHP!" ?></div>';

// evaluating the content directly result in "<div>Here is some PHP: Hello, I come from PHP!</div>"
// being echoed directly - not what you want in the middle of the CMS code!
eval(" ?>" . $someContent . "<?php ");

// with output buffering, the content is output to the buffer rather than the client,
// and it can then be captured into a variable.
ob_start();
eval(" ?>" . $someContent . "<?php ");
$evaled = ob_get_clean();

// nothing was output to the screen this time, the output was stored in the $evaled variable
echo $evaled; // <div>Here is some PHP: Hello, I come from PHP!</div>
[/code]
Copy linkTweet thisAlerts:
@NogDogMay 28.2009 — Often the first thing to do is simply review all the content being downloaded. If using Firefox you can install the YSlow add-on to help analyze this. Besides things like the "sprite" technique mentioned above for dealing with lots of little images, also look at larger images: can they be optimized to reduce their file sizes? (There are many tools on the web for image optimization, and even just experimenting with your image editor's settings for saving JPEG images can help you find a balance between quality and size.) Lastly, ask yourself if you really need all of those images.

Look at the text files that are being served up: HTML, CSS, JavaScript. Are any of them bloated with things you don't need? After you've removed any useless "fat", you can look into content-negotiation to transmit them as compressed files. You can use PHP to help you out. For instance, your CSS can be accessed as a .php file (instead of .css or whatever):

style.php:
[code=php]
<?php
header('Content-Type: text/css');
ob_start('ob_gzhandler'); // that's it, all you have to do!
?>
body {
margin: 0;
padding: 0;
}
/* etc.... */
[/code]
×

Success!

Help @bejitto101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...