<?xml version="1.0"?><rss version="2.0"><channel><title>Comments on Caching output in PHP - ILoveJackDaniels.com</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/</link><description>Latest comments on Caching output in PHP on ILoveJackDaniels.com</description><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Dave ( &lt;a href="http://"&gt;http://&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Just wanted to correct/clarify my previous post, the problem of multiple access would be twofold:&lt;br /&gt;
&lt;br /&gt;
1.) Processes might read a partially written output file if they send a request before the output from a previous request has finished writing&lt;br /&gt;
2.) Processes might request the same file and write to the same output file simultaneously. This might be ok but I'm not sure - both processes should end up writing the same data at the same places if the request is the same, but there might be some ordering issues if you just changed the file and the old version is in the middle of being cached, for example.&lt;br /&gt;
&lt;br /&gt;
So I think you definitely want to do atomic rename..</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Dave ( &lt;a href="http://"&gt;http://&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Also, someone brought up a good point that there could be more than one thread (usually, a &quot;process&quot; though, not a thread) writing to a file at the same time. You should probably write the cached contents to a temporary file and then do an atomic rename to the cached file.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Dave ( &lt;a href="http://"&gt;http://&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;This looks very useful, however I think you should check if the method is POST, and then skip caching altogether.  Someone else suggested putting the $_POST variables inside the md5sum, but that's not correct with respect to HTTP - you may be posting the same variables, but you don't want the request to be ignored, the side effects of the request still have to happen. (Maybe there's some variables in $_POST that change with each request anyway, but that's rather hacky to rely on). Instead should probably wrap the begin_caching.php and end_caching.php with a check for the server method. Like this:&lt;br /&gt;
&lt;br /&gt;
if ($_SERVER['REQUEST_METHOD'] == 'GET') {&lt;br /&gt;
   /* rest of begin_caching.php here */&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
And similarly for end_caching.php.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Kit ( &lt;a href="http://www.reapermini.com"&gt;http://www.reapermini.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Thanks for this starting point, Dave. I tweaked it a smidgen for my purposes.  I was able to get it added to my main site template and away we go.&lt;br /&gt;&lt;br /&gt;@Steve Exley, before I was cruising at about 80% CPU capacity on my server during peak hours, and now I'm down about half that (steady traffic: 1.5 million page views a month).  I changed the time variable to 30 minutes.  Scientific, no?  : ]</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by csjoe ( &lt;a href="http://"&gt;http://&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;I have encountered an Internal Server Error after placing both the files in my root directory and also created the folder cache. May I know what went wrong? Did anyone faced the same issue too? Thank you.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Steve Exley ( &lt;a href="http://www.studio-40.com"&gt;http://www.studio-40.com&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Has anybody done any performance testing with this??  &lt;br /&gt;&lt;br /&gt;Obviously the results would be heavily influenced by the time taken to produce the original page.  It would still be interesting to know what factor of gain we could expect to see by caching pages.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Han ( &lt;a href="http://"&gt;http://&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;I often get empty pages in the cached files. I think I'm having issues when two visistors simultaneously open a non-cached page resulting in a bad/empty cache page.&lt;br /&gt;Is this possible? Does anyone have any suggestions?&lt;br /&gt;After emptying the cache everything works fine again.</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Mike ( &lt;a href=""&gt;&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;How does PHP handle simultaneous requests?  Can their be issues with two threads writing to the same cache file at the same time?  Or does PHP only process 1 request at a time?</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Kristian Tørning ( &lt;a href="http://www.protos.dk/"&gt;http://www.protos.dk/&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;Thanks a lot for this script. It made life much easier&lt;br /&gt;&lt;br /&gt;I love the design. &lt;br /&gt;&lt;br /&gt;Create two files. Include one above and one below.&lt;br /&gt;&lt;br /&gt;Watch the magic!&lt;br /&gt;&lt;br /&gt;I have a small site cooking at http://www.kaffenyheder.dk - its a basic RSS agregation site for danish news. &lt;br /&gt;&lt;br /&gt;Applying the script made it FAST!&lt;br /&gt;&lt;br /&gt;Naturally, when the cache needs to be updated the page will hang a bit, but that is just for one unlucky user every 5 minutes or so. &lt;br /&gt;&lt;br /&gt;For the rest of the users its just nice.&lt;br /&gt;&lt;br /&gt;Again. Thank you.&lt;br /&gt;&lt;br /&gt;Kind regards&lt;br /&gt;&lt;br /&gt;Kristian</description></item><item><title>Comment on Caching output in PHP</title><link>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</link><guid>http://www.ilovejackdaniels.com/article/caching-output-in-php/comments/</guid><description>Comment by Tien Manh ( &lt;a href="http://hangdep.net"&gt;http://hangdep.net&lt;/a&gt; )&lt;br /&gt;&lt;br /&gt;&lt;?&lt;br /&gt;$cacheDir  = dirname(__FILE__) . '/cache/home/';&lt;br /&gt;if (isset($_GET['page'])) {&lt;br /&gt;	$cacheFile = $cacheDir . '_' . $_GET['page'] . '.html';&lt;br /&gt;} else {&lt;br /&gt;	$cacheFile = $cacheDir . 'index.html';&lt;br /&gt;}	&lt;br /&gt;if (file_exists($cacheFile))&lt;br /&gt;{&lt;br /&gt;	header(&quot;Content-Type: text/html&quot;);&lt;br /&gt;	readfile($cacheFile);&lt;br /&gt;	exit;&lt;br /&gt;} else {&lt;br /&gt;include('library/home.php');&lt;br /&gt;}&lt;br /&gt;?&gt;     &lt;br /&gt;&lt;br /&gt;&lt;? ob_start(); ?&gt;&lt;br /&gt;&lt;br /&gt;&lt;?&lt;br /&gt;$buffer = ob_get_contents();&lt;br /&gt;ob_end_flush();&lt;br /&gt;$fp = @fopen($cacheFile, &quot;w&quot;);&lt;br /&gt;@fwrite($fp, $buffer);&lt;br /&gt;@fclose($fp);&lt;br /&gt;?&gt;&lt;br /&gt;&lt;br /&gt;Demo : http://hangdep.net</description></item></channel></rss>