<?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>Gabriel Crowe &#187; dynamic</title>
	<atom:link href="http://www.gabrielcrowe.co.uk/tag/dynamic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gabrielcrowe.co.uk</link>
	<description>Programmer, Designer, Consultant and Full of Ideas.</description>
	<lastBuildDate>Fri, 29 Jan 2010 17:01:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Speeding up your PHP application with full page caches.</title>
		<link>http://www.gabrielcrowe.co.uk/2010/01/22/speeding-up-your-php-application-with-full-page-caches/</link>
		<comments>http://www.gabrielcrowe.co.uk/2010/01/22/speeding-up-your-php-application-with-full-page-caches/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 15:04:11 +0000</pubDate>
		<dc:creator>Gabriel Crowe</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.gabrielcrowe.co.uk/?p=12</guid>
		<description><![CDATA[Why cache content? Sometimes, when you are creating custom solutions in PHP, you don&#8217;t have the luxury of speedup caches in Ecommerce and CMS frameworks. The problem is that people arent always experts in database design, and SQL, so pages &#8230; <a href="http://www.gabrielcrowe.co.uk/2010/01/22/speeding-up-your-php-application-with-full-page-caches/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Why cache content?</h2>
<p>Sometimes, when you are creating custom solutions in PHP, you don&#8217;t have the luxury of speedup caches in Ecommerce and CMS frameworks. The problem is that people arent always experts in database design, and SQL, so pages with messy queries in them tend to load slowly. This can lose visitors interest, and if its ecommerce related, sales.</p>
<p>The idea is simple. Only query the databases when the content changes. If the same content is going to be fed out all the time, then why would you waste your time re-building that content, every time something hit your page?</p>
<blockquote><p>It&#8217;ll save you money if your page gets hit a lot, and it&#8217;ll make people happier when they get your page quicker.</p></blockquote>
<p>So, we <em>keep a copy</em> of the generated page first time its looked at. Using the full url as a reference, we keep them, and when someone asks for the same page again, we give them the cached copy. It&#8217;s much faster to do this.</p>
<h2>So, lets get stuck in</h2>
<p>Create a file called &#8220;cache_header.php&#8221; containing this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$starttime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$startarray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$starttime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$starttime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$startarray</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$startarray</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Settings</span>
<span style="color: #000088;">$cachedir</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache/'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Directory to cache files in (keep outside web root)</span>
<span style="color: #000088;">$cachetime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">600</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Seconds to cache files for</span>
<span style="color: #000088;">$cacheext</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Extension to give cached files (usually cache, htm, txt)</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Ignore List</span>
<span style="color: #000088;">$ignore_list</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
<span style="color: #0000ff;">'domainname.co.uk/ignorethis.php'</span><span style="color: #339933;">,</span>
<span style="color: #0000ff;">'domainname.co.uk/ignorethisaswell.php'</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Script</span>
<span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_HOST'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Requested page</span>
<span style="color: #000088;">$cachefile</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cachedir</span> <span style="color: #339933;">.</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cacheext</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Cache file to either load or create</span>
&nbsp;
<span style="color: #000088;">$ignore_page</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ignore_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #000088;">$ignore_page</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ignore_list</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> ? <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$ignore_page</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$cachefile_created</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cachefile</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ignore_page</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">@</span><span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cachefile</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">clearstatcache</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Show file from cache if still valid</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$cachetime</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$cachefile_created</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ob_gzhandler'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">readfile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cachefile</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ob_end_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// If we're still here, we need to generate a cache file</span>
&nbsp;
<span style="color: #990000;">ob_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Then create a file called &#8220;cache_footer.php&#8221; containing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000088;">$endtime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$endarray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$endtime</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$endtime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$endarray</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$endarray</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totaltime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$endtime</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$starttime</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$totaltime</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$totaltime</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;!-- This page took <span style="color: #006699; font-weight: bold;">$totaltime</span> seconds to generate --&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Now the script has run, generate a new cache file</span>
<span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cachefile</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// save the contents of output buffer to the file</span>
<span style="color: #339933;">@</span><span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #990000;">ob_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">@</span><span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">ob_end_flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>in the same folder, create a folder called &#8216;cache&#8217; and set it to writable by the php, using an FTP client:</p>
<div class="wp-caption alignnone" style="width: 269px"><img title="Screenshot of permissions" src="http://www.interact-studio.co.uk/screencast/2010-01-20_1147.png" alt="Screenshot of permissions" width="259" height="140" /><p class="wp-caption-text">Screenshot of permissions</p></div>
<p>Finally, implement the cache on your dynamic page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_header.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">//this is dynamic php</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_footer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>This effectively creates static html of a dynamic file and saves it to the cache. The cache is used if the same url is requested within a certain timeframe. This means that database intensive popular pages load instantly.</p>
<p>Good eh?</p>
<p>The real problem with this system is that it does not pay any attention to pages that are dynamically different due to logins and sessions.</p>
<p>Don&#8217;t randomly implement this on any old php software. this is a bare bones example. In order for this to work in a complex example where individuals with different credentials are likely to see different pages, you&#8217;d need to have the system aware of their credentials, and display a fresh version of the page, based on the presence of an individual. This is NOT covered in the example, and it assumes that every user gets the same page.</p>
<p>What I tend to do, is leave the main page uncached, and any elements that are custom to the user and experience uncached.</p>
<p>The included elements i will cache. For example, my popular products block, i will cache. When they are pulled into the page by the server, the server merely includes a file, instead of drawing from a database.</p>
<p>Overall this hybrid approach isnt as speedy as caching the entire page, but its very close and as a performance versus benefit tradeoff, it works very well indeed.</p>
<fb:share-button href="http://www.gabrielcrowe.co.uk/2010/01/22/speeding-up-your-php-application-with-full-page-caches/" type="box_count"></fb:share-button>]]></content:encoded>
			<wfw:commentRss>http://www.gabrielcrowe.co.uk/2010/01/22/speeding-up-your-php-application-with-full-page-caches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
