<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spoonfed Project &#187; PHP</title>
	<atom:link href="http://spoonfedproject.com/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://spoonfedproject.com</link>
	<description>Feeding developers quality resources from across the web</description>
	<lastBuildDate>Sun, 03 Apr 2011 12:34:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Dynamic page caching with PHP and output buffering</title>
		<link>http://spoonfedproject.com/php/dynamic-page-caching-with-php-and-output-buffering/</link>
		<comments>http://spoonfedproject.com/php/dynamic-page-caching-with-php-and-output-buffering/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 17:39:24 +0000</pubDate>
		<dc:creator>brobison</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[cache]]></category>

		<guid isPermaLink="false">http://spoonfedproject.com/?p=147</guid>
		<description><![CDATA[A while back I created a portfolio website for a friend of mine, Andrew Holder. As his artwork grew in popularity, traffic to his site began to erupt. The portfolio section of the site is pulled dynamically from a database. When experiencing traffic spikes, the connection to the database would fail, exceeding the maximum connections ...]]></description>
			<content:encoded><![CDATA[<p><a href="http://spoonfedproject.com/php/dynamic-page-caching-with-php-and-output-buffering" title="Dynamic page caching with PHP and output buffering"><img src="http://spoonfedproject.com/wp-content/uploads/2009/03/dynamic-page-caching.gif" alt="dynamic-page-caching" title="dynamic-page-caching" width="546" height="200" class="aligncenter size-full wp-image-148" /></a></p>
<p>A while back I created a portfolio website for a friend of mine, <a href="http://andrewholder.net/" title="Andrew Holder">Andrew Holder</a>. As his artwork grew in popularity, traffic to his site began to erupt. The portfolio section of the site is pulled dynamically from a database. When experiencing traffic spikes, the connection to the database would fail, exceeding the maximum connections allowed. Not good.</p>
<p><span id="more-147"></span></p>
<p>The website is hosted on <a href="http://mediatemple.net/webhosting/gs/" title="Media Temple">Media Temple&#8217;s Grid-Service (gs)</a> which only allows 30 database connections at a given time. This caused the site to throw a mysql_connect() error once the limit was exceeded.</p>
<p>Error: Warning: mysql_connect() [function.mysql-connect] : Too many connections</p>
<p>So, the solution seemed that I needed a way to cache these files. A few Google searches later led me to an article over at <a href="http://papermashup.com/caching-dynamic-php-pages-easily/" title="PaperMashup">PaperMashup</a>. Basically, the way it works is by utilizing <a href="http://us3.php.net/outcontrol" title="Output Buffering">PHP&#8217;s output buffering</a>. It checks to see if the page has been previously cached and if so, delivers the cached content. Otherwise, it loads the page normally, storing the output within the internal buffer. Before spitting out the contents to the user, it creates a cached version.</p>
<p>I&#8217;ve included the code snippet below similar to how it&#8217;s used at <a href="http://andrewholder.net/work.php" title="Andrew Holder">andrewholder.net</a>. For the original code and a more in depth explanation, have a look at the <a href="http://papermashup.com/caching-dynamic-php-pages-easily/" title="PaperMashup">article</a>.</p>
<pre class="brush: php">
&lt;?php
// Cache page contents
$filename	= (isset($_GET[&#039;id&#039;])) ? $_GET[&#039;id&#039;] : $cur_art;
$cache_file = &#039;cache/&#039; . $filename . &#039;.htm&#039;;
$cache_time = 3600; // 1 hour
// Serve the cached file if it&#039;s older than $cache_time
if (file_exists($cache_file) &amp;&amp;
	time() - $cache_time &lt; filemtime($cache_file)) {
	include($cache_file);
	exit;
}
ob_start();

// Page contents goes here!

$cached = fopen($cache_file, &#039;w&#039;); // Cache the contents to a file
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://spoonfedproject.com/php/dynamic-page-caching-with-php-and-output-buffering/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

