<?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>dawnerd &#187; performance</title>
	<atom:link href="http://dawnerd.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://dawnerd.com</link>
	<description>Just another WordPress site</description>
	<lastBuildDate>Fri, 13 Aug 2010 18:20:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Optimizing Dynamic Content With Memcached</title>
		<link>http://dawnerd.com/post/16_optimizing-dynamic-content-with-memcached/</link>
		<comments>http://dawnerd.com/post/16_optimizing-dynamic-content-with-memcached/#comments</comments>
		<pubDate>Sat, 14 Jun 2008 06:41:11 +0000</pubDate>
		<dc:creator>Troy Whiteley</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=16</guid>
		<description><![CDATA[I have posted before on how to use Memcached to reduce database load, but I failed to give a sound example. Hopefully this will rectify that. Please read this post to catch up if you haven&#8217;t already. otherwise I will &#8230; <a href="http://dawnerd.com/post/16_optimizing-dynamic-content-with-memcached/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I have posted before on how to use <a href="http://www.danga.com/memcached/">Memcached</a> to reduce database load, but I failed to give a sound example. Hopefully this will rectify that. Please <a href="http://dawnerd.com/php/2008/05/07/using-memcached-to-reduce-database-load/">read this post</a> to catch up if you haven&#8217;t already. otherwise I will assume you have experience with Memcached.<span id="more-16"></span></p>
<p>In this scenario, you have a custom built blog application that has a section with the latest posts. Instead of constantly pulling this text from the database or saving the output to text files, which can be slow, saving the test into memory may be the best solution.</p>
<p>Take the following code:</p>
<pre>
<code>
$sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
$query = mysql_query($sql);
if(mysql_num_rows($query)
{
     while($row=mysql_fetch_array($query))
     {
          echo "
<h2>".$row['title']."</h2>

";
     }
}
</code>
</pre>
<p>Lets make that faster.</p>
<pre>
<code>
$memcache = new Memcache;
$memcache->connect('memcache_host', 11211);
if(!$memcache->get('recent_posts'))
{
     $sql = "SELECT * FROM posts ORDER BY date LIMIT 5";
     $query = mysql_query($sql);
     $post_array = array();
     if(mysql_num_rows($query)
     {
          while($row=mysql_fetch_array($query))
          {
               $post_array[] = $row;
          }
     }
    //recent expires in one hour
    $memcache->set('recent_posts',$post_Array, 0, 3600);
}

$recent_posts = $memcache->get('recent_posts');
for($i=0;$i<count($recent_posts);$i++)
{
     echo "
<h2>".$recent_posts[$i]['title']."</h2>

";
}
</code>
</pre>
<p>You won&#8217;t notice a large increase if your site is small, but if you have a busy site and you have a bunch of spare ram, you might as well put it to use. In the future I will show you some more examples of real-world usage of <a href="http://www.danga.com/memcached/">Memcached</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/post/16_optimizing-dynamic-content-with-memcached/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
