<?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>Garrett Bluma &#187; Joomla</title>
	<atom:link href="http://garrettbluma.com/category/joomla/feed/" rel="self" type="application/rss+xml" />
	<link>http://garrettbluma.com</link>
	<description>Web Developer</description>
	<lastBuildDate>Wed, 08 Feb 2012 03:23:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Disable Mootools in Joomla 1.6</title>
		<link>http://garrettbluma.com/2010/12/18/disable-mootools-in-joomla-1-6/</link>
		<comments>http://garrettbluma.com/2010/12/18/disable-mootools-in-joomla-1-6/#comments</comments>
		<pubDate>Sun, 19 Dec 2010 01:14:38 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://garrettbluma.com/?p=404</guid>
		<description><![CDATA[Disabling Mootools in Joomla 1.5 was actually pretty simple.  All you need to to is add the following to your template above your head tag:

<pre lang="php"><?php 
    // at the top of your template
    $headers=$this->getHeadData();
    $headers['scripts'] = array();
    $this->setHeadData($headers);

    // make sure to eliminate any of the following
    //JHTML::_('behavior.framework', true);
    //JHTML::_('behavior.mootools', true);
?></pre>

In Joomla 1.6 this can remain the same if you adjust  a tiny bit of Joomla internals.  Specifically we need to modify the <tt>setHeadData()</tt> function in <tt>joomla16/libraries/joomla/document/html/html.php</tt> (around line 116).  

<pre lang="php">// original version
$this->_scripts = (isset($data['scripts']) &#038;& !empty($data['scripts'])) ? 
                  $data['scripts'] : $this->_scripts;

// updated version ( !empty() replaced with is_arrray() )
$this->_scripts = (isset($data['scripts']) &#038;& is_array($data['scripts'])) ? 
                  $data['scripts'] : $this->_scripts;</pre>

What this does is allow Joomla to accept an empty value for <tt>$headers['scripts']</tt> where it would previously reject it. 

Let me know if you have any questions.

Happy coding!]]></description>
			<content:encoded><![CDATA[<p>Disabling Mootools in Joomla 1.5 was actually pretty simple.  All you need to to is add the following to your template above your head tag:</p>
<pre class="php"><span style="color: #333333;">&lt;?php</span>
    <span style="color: #808080; font-style: italic;">// at the top of your template</span>
    <span style="color: #3333cc;">$headers</span>=<span style="color: #3333cc;">$this</span>-&gt;<span style="color: #336633;">getHeadData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #3333cc;">$headers</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span> = <a href="http://www.php.net/array"><span style="color: #333399;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #3333cc;">$this</span>-&gt;<span style="color: #336633;">setHeadData</span><span style="color: #66cc66;">&#40;</span><span style="color: #3333cc;">$headers</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #808080; font-style: italic;">// make sure to eliminate any of the following</span>
    <span style="color: #808080; font-style: italic;">//JHTML::_('behavior.framework', true);</span>
    <span style="color: #808080; font-style: italic;">//JHTML::_('behavior.mootools', true);</span>
<span style="color: #333333;">?&gt;</span></pre>
<p>In Joomla 1.6 this can remain the same if you adjust  a tiny bit of Joomla internals.  Specifically we need to modify the <tt>setHeadData()</tt> function in <tt>joomla16/libraries/joomla/document/html/html.php</tt> (around line 116).  </p>
<pre class="php"><span style="color: #808080; font-style: italic;">// original version</span>
<span style="color: #3333cc;">$this</span>-&gt;_scripts = <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #333399;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; !<a href="http://www.php.net/empty"><span style="color: #333399;">empty</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ?
                  <span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #3333cc;">$this</span>-&gt;_scripts;
&nbsp;
<span style="color: #808080; font-style: italic;">// updated version ( !empty() replaced with is_arrray() )</span>
<span style="color: #3333cc;">$this</span>-&gt;_scripts = <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #333399;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/is_array"><span style="color: #333399;">is_array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ?
                  <span style="color: #3333cc;">$data</span><span style="color: #66cc66;">&#91;</span><span style="color: #aa3333;">'scripts'</span><span style="color: #66cc66;">&#93;</span> : <span style="color: #3333cc;">$this</span>-&gt;_scripts;</pre>
<p>What this does is allow Joomla to accept an empty value for <tt>$headers['scripts']</tt> where it would previously reject it. </p>
<p>Let me know if you have any questions.</p>
<p>Happy coding!</p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2010/12/18/disable-mootools-in-joomla-1-6/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Nginx / Memcached Direct Page Caching</title>
		<link>http://garrettbluma.com/2010/08/16/nginx-memcached-direct-page-caching/</link>
		<comments>http://garrettbluma.com/2010/08/16/nginx-memcached-direct-page-caching/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 03:57:55 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://garrettbluma.com/?p=313</guid>
		<description><![CDATA[There are a few different approaches to caching; data caching, fragment caching, and full-page caching. But first, here's the typical model of a web application. User requests a page, and it works through the web server, through several layers, and finally to the database. Data caching takes the place of a database call and can be effective if there are a lot of database requests.  You would typically want to do this if you know that a piece of data is very common and unlikely to change. This saves time on one step of the process. Fragment caching is a [...]]]></description>
			<content:encoded><![CDATA[<p>There are a few different approaches to caching; data caching, fragment caching, and full-page caching. But first, here's the typical model of a web application. User requests a page, and it works through the web server, through several layers, and finally to the database.</p>
<p><img class="aligncenter size-full wp-image-331" title="Typical webapp flow chart" src="http://garrettbluma.com/wp-content/uploads/2010/08/server-caching1.png" alt="" width="602" height="101" /></p>
<p><strong>Data caching </strong>takes the place of a database call and can be effective if there are a lot of database requests.  You would typically want to do this if you know that a piece of data is very common and unlikely to change. This saves time on one step of the process.</p>
<p><img class="aligncenter size-full wp-image-332" title="Web app flowchart 2" src="http://garrettbluma.com/wp-content/uploads/2010/08/server-caching2.png" alt="" width="602" height="102" /></p>
<p><strong>Fragment caching</strong> is a little more complicated.  Instead of caching the data that you would input into another process to build a layout (for example), you just cache that layout directly.  This is great for a "featured news" section that changes only once a day and is viewed often.  It would be silly to cache that data and calculate the layout for every view, so instead we would just cache the layout itself. Saves a bunch of work.</p>
<p>Moving on, we have <strong>page caching</strong>.  Which is the topic which I really want to go over. I have a warning though, page caching is dumb. REAL dumb. But, in that magnificent dumness is speed. Page caching is the most efficient of the three. It takes the gains we get from fragment caching and goes even further. Think about it, we hardly need to do anything—and therein lies the problem, we lose ALL dynamic ability of the page. This is not an issue for a lot of sites; news pages, catalogs, and blogs can all benefit from this method of caching.</p>
<p style="text-align: center;"><img class="size-full wp-image-333  aligncenter" title="Memcache flowchart" src="http://garrettbluma.com/wp-content/uploads/2010/08/server-caching3.png" alt="" width="449" height="102" /></p>
<p style="text-align: center;">. . .</p>
<p>The method I describe here I used allowed a <em>real</em> site to receive 250,000 <em>real</em> page-views in 40 min (~104 requests/sec). I expect you can do much more. (More number towards the end.)</p>
<p>What I haven't mentioned yet is that I'm using PHP (specifically Joomla). PHP is certainly not the fastest language around, but considering the whole spectrum of issues facing a web developer, script performance is minimal. And since I'm on this rabbit trail, I'll mention that <a href="http://developer.yahoo.com/performance/rules.html">Javascript/CSS is the first thing to fix</a>, then caching. I don't need to explain more about this, there are volumes about it already.</p>
<p style="text-align: center;">. . .</p>
<p>Moving on, the method described for page caching is based on a simple fallback plan. Here is the psudo-code.</p>
<pre>If the page requested is in cache
    serve cached version
otherwise
    serve it dynamically
    add result to memcached</pre>
<p style="text-align: center;">. . .</p>
<p><strong>Joomla Plugin</strong></p>
<p>With Joomla we can actually do this very easily. We just need to hook into the <em>onAfterRender</em> event and grab the page data.  You might notice we're using the database name as the prefix to the URL in the key name. This is just to be friendly to any other sites hosted on the same server (or copies of the same site using a different database).</p>
<p>You also might notice that we're grabbing the gzipped version of the page. This is just to trim a little off what the user needs to download.</p>
<pre>// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.plugin.plugin' );

class  plgSystemFullPageMemcache extends JPlugin
{
    function plgSystemFullPageMemcache(&amp; $subject, $config) {
        parent::__construct($subject, $config);
    }

    function onAfterRender() {
        global $mainframe;
        $config = new JConfig();
        $cache_id= $config-&gt;db . ":" . $_SERVER['REQUEST_URI'];

        $data = JResponse::toString($mainframe-&gt;getCfg('gzip'));

        $db = new Memcache;
        $db-&gt;addServer('127.0.0.1', '11211', true);
        $db-&gt;set($cache_id, $data, 1, 3600);
    }
}</pre>
<p><strong>Nginx configuration</strong></p>
<pre>server {

  listen 80;
  server_name  yourdomain.com www.yourdomain.com;

  # ...

  location ~ \.php$ {
    set $memcached_key "yourdomain:$request_uri";
    memcached_pass 127.0.0.1:11211;

    default_type       text/html;
    error_page 404 405 502 = @cache_miss;
  }

  location @cache_miss {
    fastcgi_pass 127.0.0.1:9002;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/yoursite/htdocs$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
  }

  # ...
}</pre>
<p style="text-align: center;">. . .</p>
<p>Here you can see <em>@cache_miss</em> will get called when nginx can't find anything in the memcache for the given key (the url). This triggers our fallback plan where the cache will fill itself with any missing data. When we do hit the cache, we actually bypass PHP altogether. No initialization wait, no slowness, and no database calls. By educated guess, nginx and memcached in this setup can start serving content in under 15 milliseconds. Compare this with the 50-100 milliseconds we commonly see.</p>
<p>Theoretically you could serve content faster using this system than just using files due to time spent waiting on disk access. In my proof of concept, I was able to serve about 2500 requests/second but I have reason to believe that limit was imposed by the network card. The CPU was at 10% the whole time and I'm sure it could handle more.</p>
<p>Happy caching!</p>
<p>(Downloadable Plugin Coming Soon)</p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2010/08/16/nginx-memcached-direct-page-caching/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Joomla Sphinx Search Plugin</title>
		<link>http://garrettbluma.com/2010/02/16/joomla-sphinx-search-plugin/</link>
		<comments>http://garrettbluma.com/2010/02/16/joomla-sphinx-search-plugin/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 02:26:15 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Sphinx]]></category>

		<guid isPermaLink="false">http://garrettbluma.com/?p=212</guid>
		<description><![CDATA[Beware, this is beta quality software. Use at your own risk. As we understand already, Joomla doesn't have relevant searching. It's a shame since everything else is designed well, but this is a problem regarding implementation. This article explains how to setup sphinx to index your Joomla articles and use my plugin to search using Joomla. In my own experiments I built a plugin that accomplished this. It works splendidly, and I had all intention of releasing the code to the public, but got distracted. This process is currently unrefined, but it works. I'm a fan of revision anyway, so [...]]]></description>
			<content:encoded><![CDATA[<p class="dropshadow"><img class="alignnone size-full wp-image-218" title="sphinx-horse" src="http://garrettbluma.com/wp-content/uploads/2010/02/sphinx-horse.jpg" alt="" width="580" height="252" /></p>
<blockquote><p><strong>Beware, this is beta quality software. Use at your own risk.</strong></p></blockquote>
<p>As we understand already, Joomla doesn't have relevant searching. It's a shame since everything else is designed well, but this is a problem regarding implementation. This article explains how to setup sphinx to index your Joomla articles and use my plugin to search using Joomla.</p>
<p>In my own experiments I built a plugin that accomplished this. It works splendidly, and I had all intention of releasing the code to the public, but got distracted. This process is currently unrefined, but it works. I'm a fan of revision anyway, so if you see a way to improve this article, hit me up (email).</p>
<p><strong>So why doesn't Joomla have relevant searching already?</strong> — Unfortunately it doesn't seem like they had much choice about it. Joomla relies on MySQL. MySQL is just now beginning to get some Full-Text searching mechanisms implemented and those that do exist are spotty. For example, it is only available in recent versions of the MyISAM database engine and not at all for InnoDB. Plus, I've heard some complaints about it not even being terribly good at it's own job.</p>
<p>A system like Sphinx can go beyond what MySQL supports anyway. It can use word stems and substitutions, so I could specify <tt>CPP</tt> to be used interchangeably with <tt>C++</tt> in any search query and it doesn't get confused about plural or singular versions of words. It also supports weights, so I can tell Sphinx to prefer matches found in titles and move them higher in the search results.</p>
<h3>Install &amp; Configure Sphinx</h3>
<p>There are a number of good articles on the web about this. I may revisit how to install Sphinx in detail, but for now defer to the follow links:</p>
<ul>
<li><a href="http://www.sphinxsearch.com/docs/current.html#installation">Sphinx Homepage</a> — <em>Obviously the first place to start looking is the Sphinx homepage. Good documentation on Sphinx operation and internals.</em></li>
<li><a href="http://www.ibm.com/developerworks/library/os-php-sphinxsearch/">Build a custom search engine with PHP</a> — <em>IBM has a great article on using Sphinx in-the-field. This article describes setup of Sphinx as well as testing and integration with PHP.</em></li>
</ul>
<p>Here is my configuration I've been using for my Joomla Setup. This is basically the same as any configuration setup on IBM's website, but adapted to Joomla. There are a few things you'll need to edit though:</p>
<ul>
<li><tt>exampleuser</tt> — <em>This should be the username you connect with to search your database</em></li>
<li><tt>examplepass</tt> — <em>This should be the password for the username just described</em></li>
<li><tt>exampledb</tt> — <em>This is the database sphinx will be searching on.</em></li>
<li>Check your paths. I'm making some assumptions here. Your sphinx data should be outside of your web-root and readable/writable by whatever process <tt>searchd</tt> is running as.</li>
</ul>
<p><strong>/var/www/website/sphinx/config.conf:</strong></p>
<pre><code>
source mywebsite_articles
{
    type            = mysql
    sql_host        = localhost
    sql_user        = exampleuser
    sql_pass        = examplepassword
    sql_db          = exampledb
    sql_sock        = /var/run/mysqld/mysqld.sock
    sql_port        = 3306

    # indexer query
    # document_id MUST be the very first field
    # document_id MUST be positive (non-zero, non-negative)
    # document_id MUST fit into 32 bits
    # document_id MUST be unique

    sql_query = \
        SELECT `id`, `title`, `alias`, `introtext`, `fulltext`, `created`, `modified`, `hits` \
        FROM jos_content ;

    # document info query
    # ONLY used by search utility to display document information
    # MUST be able to fetch document info by its id, therefore
    # MUST contain '$id' macro
    #

    sql_query_info = \
        SELECT * \
        FROM jos_content  \
        WHERE id=$id
}
index mywebsite_articles
{
    source                  = mywebsite_articles
    path                    = /var/www/website/data
    morphology              = none

    min_word_len            = 3
    min_prefix_len          = 0
    min_infix_len           = 3
}

searchd
{
    port          = 3312
    log           = /var/www/website/sphinx/search.log
    query_log     = /var/www/website/sphinx/query.log
    pid_file      = /var/www/website/sphinx/searchd.pid
}
</code></pre>
<h3>Testing Sphinx</h3>
<p>How do we know if Sphinx is setup correctly? We test it!</p>
<p>First we need to generate the index. So fire up your terminal and run the following (assuming your sphinx install lives in /usr/local/sphinx):</p>
<pre><code>
/usr/local/sphinx/bin/indexer \
--config /var/www/website/sphinx/config.conf \
--all --rotate
</code></pre>
<p>Second we need to start the search daemon. This will process search requests.</p>
<pre><code>
/usr/local/sphinx/bin/searchd \
--config /var/www/website/sphinx/config.conf
</code></pre>
<p>If you get any errors, I'd recommend going back and reviewing the configuration and re-running the indexer, then start searchd again.</p>
<p>If we don't have any errors, we can run a test outside of Joomla to see if everything is working correctly. For example:</p>
<pre><code>/usr/local/sphinx/bin/search \
--config /var/www/website/sphinx/config.conf \
"Hello world"</code></pre>
<h3>Install &amp; Configure Joomla Plugin</h3>
<p>You can download the plugin here (<a href="http://garrettbluma.com/track.php?file=http%3A%2F%2Fgarrettbluma.com/code/sphinx/plgSphinxContent-v1.zip">Download</a>) or at the bottom of this article.</p>
<p class="dropshadow"><img class="aligncenter size-full wp-image-231" title="Sphinx Plugin Upload" src="http://garrettbluma.com/wp-content/uploads/2010/02/Screen-shot-2010-02-16-at-6.49.47-PM.png" alt="Sphinx Plugin Upload" width="528" height="206" /></p>
<p>Enable the plugin. Be sure to turn off other search modules while you're at it. It is much easier to determine if your search results are isolated to one plugin.</p>
<p class="dropshadow"><img class="aligncenter size-full wp-image-232" title="Enable Plugin" src="http://garrettbluma.com/wp-content/uploads/2010/02/Screen-shot-2010-02-16-at-6.50.51-PM.png" alt="Enable Plugin" /></p>
<p class="dropshadow"><img class="aligncenter size-full wp-image-235" title="Configure Sphinx Plugin" src="http://garrettbluma.com/wp-content/uploads/2010/02/Screen-shot-2010-02-16-at-6.52.43-PM.png" alt="Configure Sphinx Plugin" width="514" height="229" /></p>
<p>If you are hosting Sphinx on the same server as your web service, you'll probably want to keep these similar to what I have. Otherwise, be sure to fill-in the appropriate fields here and configure any firewalls that may be between the web server and the Sphinx server.</p>
<p>You will also want to be careful to use the same name of your index defined in your config file. In my example I'm using <tt>mywebsite_articles</tt>. This is what I fill-in for <em>Resource Name.</em></p>
<h3>Test Sphinx Plugin</h3>
<p>The only thing left to do now is to actually test the thing. Go to your website and do a search!</p>
<h3>Completing the solution</h3>
<p>There are a few things still missing from this setup that I want you to be aware of.</p>
<p>First, if you don't update your index periodically, you will only see the content that you indexed the first time. One solution is to setup a scheduled task (or cron) to re-index your content. Notice as well I put <tt>--rotate</tt> at the end of my indexing operation. Once Sphinx has updated the index, it will begin to use the new index to search with. You don't need to restart your <tt>searchd</tt> process, just update the index.</p>
<p>Second, you should be aware that if your server restarts you'll need <tt>searchd</tt> to start automatically. An init script would work great, even something as simple as this:</p>
<p><strong>/etc/init.d/searchd</strong></p>
<pre><code>
#! /bin/sh
NAME=sphinx
DAEMON=/usr/local/sphinx/bin/searchd
CONFIG=/var/www/website/sphinx/config.conf
[ -x "$DAEMON" ] || exit 0
case "$1" in
  start)
    echo "Stopping any running daemons..."
    $DAEMON --config $CONFIG --stop
    echo "Starting sphinx search daemon..."
    $DAEMON --config $CONFIG
    ;;
  stop)
    echo "Stopping sphinx search daemon..."
    $DAEMON --config $CONFIG --stop
    ;;
  *)
    echo "Usage: $NAME {start|stop}"
    exit 3
    ;;
esac
:
</code></pre>
<p><div class='download-link'>
							<a href='http://garrettbluma.com/track.php?file=http%3A%2F%2Fgarrettbluma.com/code/sphinx/plgSphinxContent-v2.zip'><img alt='Download' class='leftalign' src='http://garrettbluma.com/wp-content/plugins/dBeautifier/icons/tar.png' /></a>
							<h4>
								<a href='http://garrettbluma.com/track.php?file=http%3A%2F%2Fgarrettbluma.com/code/sphinx/plgSphinxContent-v2.zip'>plgSphinxContent-v2.zip (Sphinx Search Plugin)</a>
							</h4><p>Downloads: 162 File Size: 0.0 KB </p>
						</div></p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2010/02/16/joomla-sphinx-search-plugin/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Caching in Joomla 1.5 with Fault Tolerance</title>
		<link>http://garrettbluma.com/2009/12/20/programmatic-caching-in-joomla-1-5-with-fault-tolerance/</link>
		<comments>http://garrettbluma.com/2009/12/20/programmatic-caching-in-joomla-1-5-with-fault-tolerance/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 18:00:43 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.garrettbluma.com/?p=154</guid>
		<description><![CDATA[If you've used Joomla before, you may be aware that there are built-in caching features which you can activate to reduce the number of expensive database calls needed to render a page. This feature is also available to your own custom code, whether that be in the form of components, modules, or plugins. The actual mileage for this feature may vary due to the speed of the server and what Joomla decides to cache. Personally I think Joomla's use of caching, specifically how they use it for articles, leaves something to be desired. The downsides include content that doesn't get [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align:center"><img src="http://www.garrettbluma.com/images/posts/fault-tolerant-caching-overview.png" alt="Overview" /></p>
<p>If you've used Joomla before, you may be aware that there are built-in caching features which you can activate to reduce the number of expensive database calls needed to render a page. This feature is also available to your own custom code, whether that be in the form of components, modules, or plugins.</p>
<p>The actual mileage for this feature may vary due to the speed of the server and what Joomla decides to cache. Personally I think Joomla's use of caching, specifically how they use it for articles, leaves something to be desired. The downsides include content that doesn't get flushed out once it has changed or content that has too short of a lifetime to prove useful. Because of this, I don't think lot of developers realize that you can use it to cache your own components, plugins, and modules reliably and without affecting the user experience in a negative way, all the while in a modest environment (i.e. a server environment that doesn't have special software like memcached installed.)</p>
<p>You might consider implementing a little caching if:</p>
<ul>
<li>Your code is heavy and slow <em>(Don't take it personally, mine is too sometimes)</em></li>
<li>Your code is high-latency requiring network resources that may or may not actually be available</li>
<li>You have a crazy number of visitors and your database is becoming a significant bottleneck</li>
</ul>
<p>In my own recent project I was able to improve my access times of some SOAP requests by 500% (~1 second/request down to ~200ms/request).</p>
<h3>Let's get our bearings</h3>
<p style="text-align:center"><img src="http://www.garrettbluma.com/images/posts/fault-tolerant-caching-functions.png" alt="Overview" /></p>
<p>Here's a basic example of some code which initializes a JCache object and get's some data using it. You'll notice that there are two functions, one which just generically gets some data <tt>getData</tt> and another function <tt>getDataCached</tt> which instantiates the cache and calls <tt>getData</tt>. We need to do this so that our cache is aware of what the inputs and outputs are, that way it can do a lookup on the cache and return data without ever touching <tt>getData</tt>. </p>
<p><strong>com_cachetest/cachetest.php:</strong></p>
<pre><code class="php">
class CacheTest {
    function getData($url) {
        // ... Get some data

        // in this case, I'm just returning some dummy data instead of
        // downloading a web page which if we plan for the worst case scenario,
        // might not be available.

        $data = $url;

        // send data back
        return $data;
    }
    function getDataCached($url) {

        $group = "cachetest";
        $function = array("CacheTest", "getData");

        // get a cache object and name it something useful
        $cache =&amp; JFactory::getCache( $group );
        $cache->setCaching( 1 ); // enable caching (1 = enabled, 0 = disabled)

        // call our function "getData" through the cache
        $result = $cache->call( $function , $url);

        return $result;
    }
}

$data = CacheTest::getDataCached( "http://example.com" );
var_dump($data); // to see what we actually get
</code></pre>
<h3>Fault Tolerant Caching</h3>
<p>One issue in the above example is that if <tt>getData</tt> returns some bogus data, it's going to be stuck in the cache and you won't realize it until it's too late. This isn't a likely scenario for a quick database call but if you have a network request that is waiting and waiting and eventually reaches timeout, the cached data will possibly be empty. This can be problematic but it's also easily avoided. In the following example we do a quick check to see if the data is invalid, remove the old data and get a fresh copy. </p>
<p><strong>com_cachetest/cachetest.php:</strong></p>
<pre><code class="php">
class CacheTest {
    function getData($url) {
        // ... Get some data

        // in this case, I'm just returning some dummy data instead of
        // downloading a web page which if we plan for the worst case scenario,
        // might not be available.

        $data = $url;

        // send data back
        return $data;
    }

    function getDataCached($url) {

        $group = "cachetest";
        $function = array("CacheTest", "getData");

        // get a cache object and name it something useful
        $cache =&amp; JFactory::getCache( $group );
        $cache->setCaching( 1 ); // enable caching (1 = enabled, 0 = disabled)

        // call our function "getData" through the cache
        $result = $cache->call( $function, $url);

        // Check if we get sane data. Depending on your data this
        // could be more than just an empty check
        if ( empty( $result ) ) {

            // find the id to remove data for
            $id = $cache->_makeId( $function, $url );

            // invalidate this entry in the cache
            $cache->remove( $id, $group );

            // call our function again knowing the cache has been fixed
            $result = $cache->call( $function, $url );
        }
        return $result;
    }
}
$data = CacheTest::getDataCached( "http://example.com" );
var_dump($data); // to see what we actually get
</code></pre>
<h3>In Closing</h3>
<p>Joomla's caching API is simple and, by default, just writes these cache files to disk. Although not the fastest approach the default options are really handy for a less-than-stellar server setup. These caching options are available to even shared hosts. At the same time, I should mention that Joomla has done a fantastic job of integrating support for more advanced caching systems like <a href="http://memcached.org/">memcache</a> and <a href="http://xdebug.org/">xdebug</a> so that the above code can be run even faster without any modifications.</p>
<p><div class='download-link'>
							<a href='http://garrettbluma.com/track.php?file=http%3A%2F%2Fgithub.com/gbluma/joomla-caching-example/tree/master/.git'><img alt='Download' class='leftalign' src='http://garrettbluma.com/wp-content/plugins/dBeautifier/icons/github.png' /></a>
							<h4>
								<a href='http://garrettbluma.com/track.php?file=http%3A%2F%2Fgithub.com/gbluma/joomla-caching-example/tree/master/.git'>joomla-caching-example.git (Programmatic Caching in Joomla 1.5 with Fault Tolerance)</a>
							</h4><p>Downloads: 159 File Size: 0.0 KB </p>
						</div></p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2009/12/20/programmatic-caching-in-joomla-1-5-with-fault-tolerance/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>RSForm submissions too slow!—but easy to fix!</title>
		<link>http://garrettbluma.com/2009/04/16/rsform-submissions-too-slow%e2%80%94but-easy-to-fix/</link>
		<comments>http://garrettbluma.com/2009/04/16/rsform-submissions-too-slow%e2%80%94but-easy-to-fix/#comments</comments>
		<pubDate>Thu, 16 Apr 2009 21:14:45 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[RSForm]]></category>

		<guid isPermaLink="false">http://www.garrettbluma.com/?p=123</guid>
		<description><![CDATA[Joomla is a great platform for building web sites and RSForm is a great plugin for Joomla to handle form submissions—but with a recent project we found that RSForms was having some serious trouble with only a moderate amount of data (2,000 submissions). I tried to find the source of the problem and always got to the JOIN statement where it would routinely take 10+ seconds to run one query—add in multiple queries and filters and it would take over a minute just to get 500 or so records from MySQL. I almost gave up hope of finding a solution [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://joomla.org">Joomla</a> is a great platform for building web sites and <a href="http://www.rsjoomla.com/joomla-components/rsform.html">RSForm</a> is a great plugin for Joomla to handle form submissions—but with a recent project we found that RSForms was having some serious trouble with only a moderate amount of data (2,000 submissions).</p>
<p>I tried to find the source of the problem and always got to the JOIN statement where it would routinely take 10+ seconds to run one query—add in multiple queries and filters and it would take over a minute just to get 500 or so records from MySQL.</p>
<p>I almost gave up hope of finding a solution and resolved to rewrite the entire search &amp; display module, but then I decided to check the indexes—an amateur mistake not looking at this sooner.</p>
<p>Sure enough, no indexes on the fields we join the tables on.</p>
<p>To fix the problem add the following indexes (remember, we're not talking about Primary Keys or Unique indexes here):</p>
<p>on <tt>jos_rsform_submissions</tt> (table name may be different) add an index to the FormId field.</p>
<p>on <tt>jos_rsform_submission_values</tt> (table name may also be different) add an index to the SubmissionId field.</p>
<p>or in SQL:</p>
<pre>ALTER TABLE `jos_rsform_submissions` ADD INDEX ( `FormId` )
ALTER TABLE `jos_rsform_submission_values` ADD INDEX ( `SubmissionId` )</pre>
<p>Those queries which were taking 1 minute to run now take roughly 0.5 seconds. The queries are really inefficient but I can live with requests under a second.</p>
<p>Now I can get back to enjoying the Joomla community—they really are awesome!</p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2009/04/16/rsform-submissions-too-slow%e2%80%94but-easy-to-fix/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Joomla 1.5 Sphinx Search Plugin</title>
		<link>http://garrettbluma.com/2009/03/08/joomla15-sphinx-plugin/</link>
		<comments>http://garrettbluma.com/2009/03/08/joomla15-sphinx-plugin/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 10:15:35 +0000</pubDate>
		<dc:creator>Garrett Bluma</dc:creator>
				<category><![CDATA[Joomla]]></category>
		<category><![CDATA[Joomla 1.5]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Sphinx]]></category>

		<guid isPermaLink="false">http://www.gbluma.com/?p=16</guid>
		<description><![CDATA[I just finished a large chunk of work on my own full-text search plugin for Joomla. It works in conjunction with Sphinx to index and search Joomla content "the right way." (i.e. not using basic LIKE statements) Edit (Feb. 27, 2010): I just released a beta version of this plugin here: Joomla Sphinx Search Plugin Joomla has been notorious for not being able to sort search results by relevancy and really, it's not very fast either. Sphinx allows for full-text searching on any type of database table (so you're not forced to use MyISAM), and can even work with PostgreSQL [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignright size-full wp-image-18" title="Sphinx Logo" src="http://garrettbluma.com/wp-content/uploads/2009/03/sphinx.jpg" alt="Sphinx Logo" width="200" height="51" />I just finished a large chunk of work on my own full-text search plugin for <a href="http://www.joomla.org">Joomla</a>.</p>
<p>It works in conjunction with <a href="http://www.sphinxsearch.com/">Sphinx</a> to index and search Joomla content "the right way." (i.e. not using basic LIKE statements)</p>
<blockquote><p>Edit (Feb. 27, 2010): I just released a beta version of this plugin here: <a href="http://garrettbluma.com/2010/02/16/joomla-sphinx-search-plugin/">Joomla Sphinx Search Plugin</a></p></blockquote>
<p><a href="http://www.joomla.org">Joomla</a> has been notorious for not being able to sort search results by relevancy and really, it's not very fast either.</p>
<p><a href="http://www.sphinxsearch.com/">Sphinx</a> allows for full-text searching on any type of database table (so you're not forced to use MyISAM), and can even work with PostgreSQL (which Joomla unfortunately doesn't support.) It can handle a number of advanced features like:</p>
<ul>
<li>Wildcard searching</li>
<li>English word stems ('dogs' =&gt; 'dog')</li>
<li>Stopwords ( so that words like 'the' don't get searched)</li>
<li>Equivilent word matching ( cpp = c++ = cplusplus )</li>
<li>Live index updates ( although my plugin does this manually )</li>
</ul>
<p>The project I'm working on is a three part system, including:</p>
<ul>
<li>Search plugin – to take the search terms and call the Sphinx search daemon.</li>
<li>Content plugin – to update the Sphinx index when a new article has been written or updated.</li>
<li>Admin Component – to help maintain, help configure,  and assist in setup of the index.</li>
</ul>
<p>I don't know when this project will be released in beta, but hopefully by next week I can get something cobbled together. Let me know (<a href="mailto:garrett.bluma@gmail.com">Email me</a>) if you're interested because I want this project to be as usable a possible and would very much appreciate the input.</p>
]]></content:encoded>
			<wfw:commentRss>http://garrettbluma.com/2009/03/08/joomla15-sphinx-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using memcached
Database Caching 5/34 queries in 0.007 seconds using memcached
Object Caching 421/472 objects using memcached

Served from: garrettbluma.com @ 2012-02-08 01:06:25 -->
