<?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>Voices &#187; Programming Languages</title>
	<atom:link href="http://voices.ghostmotion.com/category/programming-languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://voices.ghostmotion.com</link>
	<description>Ghostmotion&#039;s blog, white-papers, and discussion on Software, Technology, and Automation</description>
	<lastBuildDate>Sat, 29 May 2010 20:36:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>PhaseCache &#8211; A great way to Cache PHP, and finally a reason to use ASP style tags!</title>
		<link>http://voices.ghostmotion.com/phasecache-a-great-way-to-cache-php-and-finally-a-reason-to-use-asp-style-tags/</link>
		<comments>http://voices.ghostmotion.com/phasecache-a-great-way-to-cache-php-and-finally-a-reason-to-use-asp-style-tags/#comments</comments>
		<pubDate>Mon, 25 May 2009 12:40:38 +0000</pubDate>
		<dc:creator>laurei</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Proof of concept]]></category>
		<category><![CDATA[White Papers]]></category>

		<guid isPermaLink="false">http://voices.ghostmotion.com/?p=3</guid>
		<description><![CDATA[PHP phaseCache is nifty method of caching PHP using ASP style tags, cURL, and apache's .htaccess directives.

When you read this you'll discover two things: 1.) It's VERY simple, and 2.) Yes, finally found a reason for ASP style tags!]]></description>
			<content:encoded><![CDATA[<h2>What is PHP phaseCache?</h2>
<p>PHP phaseCache is nifty method of caching PHP using <strong>ASP</strong> style tags,<strong> cURL</strong>, and apache&#8217;s <strong>.htaccess</strong> directives.</p>
<p>When you read this you&#8217;ll discover two things: 1.) It&#8217;s VERY simple, and 2.) Yes, finally found a reason for ASP style tags!</p>
<h2>What are the advantages of a phase cache?</h2>
<p>Maybe let&#8217;s look at the disadvantages of the current caching systems to find out why I felt I needed to do something different:</p>
<ul>
<li>Caching systems like smartycache mostly have the disadvantage of being based on time, that ALL your information is tarred with the same brush, it&#8217;s either all out of date, or all in date, it just seems so clumsy.</li>
<li>Time is lost every time the cache is rebuilt, typically before a client request is returned.</li>
<li>Some calculations in some pages only ever need to be done once, why force the cache to recalculate these things every time the cache expires?</li>
<li>There are some things which you WANT php to recalculate every time the page loads, most caching systems just won&#8217;t work like that.</li>
<li>You gain quite a bit of overhead running the caches themselves.</li>
</ul>
<p>Ok so let&#8217;s look at the advantages of phase caching system.</p>
<ul>
<li>You gain a lot of freedom by not having to optimise all of your code for the runtime, you can really keep alot more code in your files, eg. different versions of config files according to variables like the $_SERVER['SERVER_NAME'];</li>
<li>You can still use it with other caching systems alongside phaseCache.</li>
<li>PhaseCache uses .htaccess to check for cached/uncached versions, not php</li>
<li>You also don&#8217;t have to include any files at all or make any structural change to your php files, simply just change the tag type to tell the compiler if you want it parsed now, or later..</li>
<li>Simple, just drop a few files in the right places, don&#8217;t go messing about with packages.</li>
</ul>
<p>Possible disadvantages</p>
<ul>
<li>You&#8217;ll need to be running MOD_PHP, as the flag directive won&#8217;t work with PHP-CGI</li>
<li>There is a possibility that I have re-invented the wheel, if so please tell me nicely.</li>
</ul>
<p>Things I have considered that may be a disadvantage but I don&#8217;t care about</p>
<ul>
<li>short php tags, asp tags etc: my justification is that there are PLENTY of projects that won&#8217;t just run &#8220;out of the box&#8221; on any web server due to their dependencies, eg GD stuff, PECL stuff, PEAR stuff etc.</li>
</ul>
<h2>How does phaseCache work?</h2>
<p>What we are going to do is take php files, and parse them partly, using <strong>&lt;?php ?&gt;</strong> or <strong>&lt;? ?&gt;</strong> tags (I don&#8217;t disagree with the use of short tags so long as you know the disadvantages), and then store the results in a cached folder for a second phase parsing as the client actually requests it, this time using the unparsed <strong>&lt;% %&gt;</strong> (asp style tags).</p>
<p>A very short source file may look like</p>
<pre class="php">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
&lt;?
include('something');
$adjective=doTenThousandCalculations_IncludesAndMoreThatTakesAlotOfTimeAndResources_FindingAnAdjectiveToDescribeTheWorld();
?&gt;
Hello &lt;?=$adjective?&gt; world,  it is &lt;%=time()%&gt; seconds since I saw you in flares.
&lt;/body&gt;&lt;/html&gt;</pre>
<p>Which creates and stores a file that looks like:</p>
<pre class="php">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
Hello beautiful world, it is  &lt;%=time()%&gt; seconds since I saw you in flares.
&lt;/body&gt;&lt;/html&gt;</pre>
<p>And finally when the 2nd file is actually parsed it will look like</p>
<pre class="php">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
Hello beautiful world, it is 1243209600 seconds since I saw you in flares.
&lt;/body&gt;&lt;/html&gt;</pre>
<p>Your directory layout should look like this:</p>
<pre>/www/
/www/source/
/www/cached/</pre>
<p>Obviously the /source/ directory holds the source files, and /cached/ directory holds your cached files with partly rendered pages.</p>
<p>You may be wondering how php parses the asp tags, in the cached dir but  it doesn&#8217;t in the source dir, but its really simple: a line in your .htaccess file with &#8220;php_flag    asp_tags    <strong>on</strong>&#8220;  or <strong>off</strong> will change the php parsing behavior in the directory it resides, what we do is put asp_tags to <strong>off </strong>in the source directory and <strong>on </strong>in the cache directory.</p>
<p>Check out this directory layout:</p>
<pre>/www/
/www/source/
/www/source/.htacess (includes the line: php_flag asp_tags off)
/www/cached/
/www/cached/.htaccess (includes the line: php_flag asp_tags on)</pre>
<p>By now not much should happen, apart from any files served from /source/, and /cached/ sould render differently.</p>
<p>Now the good stuff happens.</p>
<p>Put a .htaccess file in the root of your website (that is /www/.htaccess), make it do these things:</p>
<ol>
<li>Ignore any files that are actually in the /www/ directory eg. images, javascript, etc that won&#8217;t be cached.</li>
<li>Check if the requested file is already in the /cached/ directory
<ol>
<li>If it is; rewrite the url so that the file is served from /cached/ but looks to the client like it&#8217;s in /www/</li>
<li>If the file isn&#8217;t already in /cached/, run a script &#8211; let&#8217;s call it /www/www.php &#8230;</li>
</ol>
</li>
</ol>
<p>/www/www.php will do the following:</p>
<ol>
<li>Use <strong>cURL</strong> to request the file from the /source/ directory, remember that because it&#8217;s rewritten, www.example.com/index.php will be requested as www.example.com/source/index.php</li>
<li>You want the file as a string, looking at the string, note that the &lt;? ?&gt; or &lt;?php ?&gt; tags have been parsed but the file contains unparsed &lt;% %&gt; tags.</li>
<li>Have the script save the file to it&#8217;s namesake in the /www/cached/ folder</li>
<li>Pretty much done, send a redirect header to the SAME url</li>
<li>Now the file exists in the /www/cached/ folder, the webserver will find it after the client redirects to the same url; as it will be partly parsed, only what is inside the &lt;% %&gt; tags will need to be parsed by php this time.</li>
</ol>
<p>That&#8217;s pretty much the esscence of it</p>
<p>Here is another more complete run-down of the files needed and what they do.</p>
<pre>/www/
/www/.htaccess (this file controls the redirection)
/www/www.php (this file creates the cached files)
/www/source/
/www/source/.htacess (includes the line: php_flag asp_tags off)
/www/cached/
/www/cached/.htaccess (includes the line: php_flag asp_tags on)</pre>
<p>I have included the files as a zip for you to peruse, any comments are welcome, I know this could be more secure but it&#8217;s just proof of concept. Any comments or suggestions on how to deliver this article more clearly are welcome.</p>
<p><a href="http://voices.ghostmotion.com/wp-content/uploads/2009/05/www.zip">source code for phasecache</a></p>
<h2>What would be a better way of caching PHP other than phased caching systems such as PHP Phasecache?</h2>
<p>Some kind of &lt;?runat directive in php.</p>
<p>PhaseCache 2.0, which is a working proof of concept code already, phaseCache 2.0 allows storing different cached pages based on server variables eg &#8216;SERVER_NAME&#8217;, also takes care of g-zipping files and serving different content to different clients as they can handle it, will be released if the demand justifies running example code.</p>
]]></content:encoded>
			<wfw:commentRss>http://voices.ghostmotion.com/phasecache-a-great-way-to-cache-php-and-finally-a-reason-to-use-asp-style-tags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

