<?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>Code Fury &#187; CodeIgniter</title>
	<atom:link href="http://codefury.net/category/php-development/codeigniter/feed/" rel="self" type="application/rss+xml" />
	<link>http://codefury.net</link>
	<description>One programmer's formatted output stream</description>
	<lastBuildDate>Tue, 22 Jun 2010 23:36:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Flushing CodeIgniter&#8217;s URI-based Cache (Part I)</title>
		<link>http://codefury.net/2009/12/flushing-codeigniters-uri-based-cache-part-i/</link>
		<comments>http://codefury.net/2009/12/flushing-codeigniters-uri-based-cache-part-i/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 04:49:33 +0000</pubDate>
		<dc:creator>Kenny Katzgrau</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://codefury.net/?p=152</guid>
		<description><![CDATA[CodeIgniter&#8217;s output caching mechanism &#8212; at least in my opinion &#8212; has limited usefulness. It can be used to cache the final payload sent to the user for a given number of minutes. But sometimes clearing the cache for all pages or a specific page can be useful, especially if keeping the user from seeing [...]]]></description>
			<content:encoded><![CDATA[<p>CodeIgniter&#8217;s output caching mechanism &#8212; at least in my opinion &#8212; has limited usefulness. It can be used to cache the final payload sent to the user for a given number of minutes. But sometimes clearing the cache for all pages or a specific page can be useful, especially if keeping the user from seeing stale data is important.</p>
<p>This post will discuss what caching is, and how it is used in CodeIgniter. The next post will provide a few helper methods for clearing caches for specific pages (or really, URIs). I think this will make CI&#8217;s caching feature a lot more usable.</p>
<p>In a nutshell, this is how (and why) output caching is typically implemented:</p>
<p>Suppose we have a page at http://mysite.com/stats/website which loads and shows all the statistics about the users on my site, activity, files posted, etc. In other words, a lot of SQL queries (maybe 40 or so) and calculations go into this page. That takes significant time and resources.</p>
<p>Now imagine that this page is particularly popular. Maybe a heavily-read blog or bookmarking service links to your website, and you receive an unexpected burst in traffic. All of those page loads are re-running your calculations and queries, and that is consuming serious memory and CPU cycles. The number of requests your website can serve per-minute is now falling through the floor!</p>
<p>The idea of caching involves using the fact that our page probably doesn&#8217;t need calculate those statistics or run those queries every time. Perhaps gathering all of that data once an hour would suffice.</p>
<p>&#8220;Caching&#8221; involves generating the statistics page like we normally would, but saving a copy of the output we were going to send to the user for a certain amount of time. During that time period, everyone who requests that page will get the same page output that we generated x minutes ago.</p>
<p>After that time period passes, and we receive another page request, we recalculate the stats, and send them to the user. Of course, we&#8217;ll save the newer stats for a given amount of time for subsequent requests.</p>
<p>This basic caching &#8212; &#8220;page caching&#8221; &#8212; is the kind that CodeIgniter offers.</p>
<p>When you are inside a controller, you can tell CodeIgniter that you want to cache any requests to the current URI for <strong>n</strong> minutes. For that time period, you only want to send the output your are about to generate.</p>
<p>This is how it looks:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Stats <span style="color: #000000; font-weight: bold;">extends</span> Controller
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> website<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 40 SQL Queries (Maybe a little overboard)</span>
    <span style="color: #666666; font-style: italic;">// Serious, ultra-precise calculations</span>
    <span style="color: #666666; font-style: italic;">// More intensive code that isn't good for fast page loads</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Tell CI you want to cache the output of this page for 10 minutes.</span>
    <span style="color: #666666; font-style: italic;">// This means that for the next 10 minutes, when CodeIgniter receives</span>
    <span style="color: #666666; font-style: italic;">// a request for stats/website, the CI system will not even consult this</span>
    <span style="color: #666666; font-style: italic;">// method or instantiate this controller. It will serve a static page containing</span>
    <span style="color: #666666; font-style: italic;">// the exact content that you output here.</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cache</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'stats/website'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that&#8217;s all you have to do! In this example, CodeIgniter&#8217;s output caching works great.</p>
<p>So then, what were the qualms I mentioned earlier?</p>
<p>Suppose we also cache user profile pages, which might be located at a URI similar to http://mysite.com/users/profile/kennyk. For whatever reason, we will assume that caching this page could be very beneficial due to heavy traffic on the site.</p>
<p>If a user profile is cached for 10 minutes, and a user updates his profile in some profile editing area, he will still see the older version of his profile page when he tries to view it. This can be confusing and inconvenient.</p>
<p>Other MVC frameworks (like Rails) handle this issue in a snap by allowing the developer to &#8220;sweep&#8221; caches at will. CodeIgniter does not offer any functionality similar to this.</p>
<p>My next post will document a cache helper I&#8217;ve whipped up to allow CodeIgniter to sweep caches for a given URI. Look out for &#8220;Flushing CodeIgniter&#8217;s URI-based Cache (Part II).&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://codefury.net/2009/12/flushing-codeigniters-uri-based-cache-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enable Site-Wide Profiling With CodeIgniter</title>
		<link>http://codefury.net/2009/11/enable-site-wide-profiling-with-codeigniter/</link>
		<comments>http://codefury.net/2009/11/enable-site-wide-profiling-with-codeigniter/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 18:35:10 +0000</pubDate>
		<dc:creator>Kenny Katzgrau</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[hooks]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[site]]></category>
		<category><![CDATA[wide]]></category>

		<guid isPermaLink="false">http://codefury.net/?p=121</guid>
		<description><![CDATA[A very cool part of CodeIgniter is its ability to give you the &#8216;profiling&#8217; information for page loads. That is, if you add:

$this-&#62;output-&#62;enable_profiler&#40;true&#41;;

In your controller before you load a view, CodeIgniter will give you information regarding how fast the page loaded, how many SQL queries executed, the content of each query, and the running time [...]]]></description>
			<content:encoded><![CDATA[<p>A very cool part of CodeIgniter is its ability to give you the &#8216;profiling&#8217; information for page loads. That is, if you add:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enable_profiler</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In your controller before you load a view, CodeIgniter will give you information regarding how fast the page loaded, how many SQL queries executed, the content of each query, and the running time of each query. This is incredibly useful when you are trying to debug your application, or simply see how quickly things are loading.</p>
<p>There&#8217;s only one problem: To enable profiling, that line of code above must be present. What if you want to profile several pages, or even your whole web application? In that case, you have to start thinking:</p>
<ul>
<li>I could put that line in the constructor of my controller, and then of of that controller&#8217;s methods will be profiled.</li>
<li>I could put that line in each method I want to profile.</li>
</ul>
<p>These methods start to get ugly. And of course, you don&#8217;t want to comment out each profiling line when you don&#8217;t need them.</p>
<p>My method to tackle this problem involves using CodeIgniter&#8217;s Hooks feature to enable or disable profiling for the entire web applications based on a value in the configuration file. In the end, I can turn on profiling for my entire website via a config value by setting it to true or false. This is my method:</p>
<p>1. Enable hooks in your CodeIgniter Application by going to config/config.php end setting the flag to true:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'enable_hooks'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #339933;">;</span></pre></div></div>

<p>2. Create a file in the config directory named <strong>hooks.php</strong> if it does not already exist. Inside it, place:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$hook</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'post_controller_constructor'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                <span style="color: #0000ff;">'class'</span>    <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ProfilerEnabler'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'function'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'EnableProfiler'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'filename'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hooks.classes.php'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'filepath'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hooks'</span><span style="color: #339933;">,</span>
                                <span style="color: #0000ff;">'params'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>3. Create a folder in your application directory named <strong>hooks </strong>(If it does not already exist). Inside it, create a file named <strong>hooks.classes.php</strong>. Inside it, put:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ProfilerEnabler
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> EnableProfiler<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$CI</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>get_instance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">output</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">enable_profiler</span><span style="color: #009900;">&#40;</span> config_item<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'enable_profiling'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>4. Finally, add this line to your application&#8217;s main configuration file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'enable_profiling'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>Setting this value to true will enable profiling across your entire website. Now, you can zip through pages, checking the load times of each without any trouble!</p>
<p>Note: I&#8217;ve always thought that an additional way to do this would be to extend the Controller class and enable profiling from there. But this method, I think, has the least impact on existing code.</p>
]]></content:encoded>
			<wfw:commentRss>http://codefury.net/2009/11/enable-site-wide-profiling-with-codeigniter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
