<?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; memcached</title>
	<atom:link href="http://dawnerd.com/tag/memcached/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>
		<item>
		<title>Using Memcached to Reduce Database Load</title>
		<link>http://dawnerd.com/post/6_using-memcached-to-reduce-database-load/</link>
		<comments>http://dawnerd.com/post/6_using-memcached-to-reduce-database-load/#comments</comments>
		<pubDate>Thu, 08 May 2008 05:00:00 +0000</pubDate>
		<dc:creator>Troy Whiteley</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[server loads]]></category>

		<guid isPermaLink="false">http://dawnerd.com/?p=6</guid>
		<description><![CDATA[If you host your site on a shared server environment, then this information will not be useful for you (unless the shared server had memcached installed, which would be awesome). If you run a large website, you probably should be &#8230; <a href="http://dawnerd.com/post/6_using-memcached-to-reduce-database-load/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you host your site on a shared server environment, then this information will not be useful for you (unless the shared server had memcached installed, which would be awesome). <span id="more-6"></span></p>
<p>If you run a large website, you probably should be offsetting your database load. One popular solution is to cache database queries to files on the server&#8217;s hardrive. This is not the best way if you are looking for near instant data read times. Memcached stores data into ram, making reading and writing the data very quick.</p>
<p>A simple scenario: You increment the &#8216;views&#8217; field for a news post. All growing traffic causes a ton of updates which result in locking. Your site becomes slow. Memcached to the rescue!</p>
<pre>
<code markup="none">
$memcache = new Memcache;
$memcache->connect('memcache_host', 11211);
if(!$memcache->get('postxyz_123_views'))
{
    //sql to grab number of views

    //number expires in one hour
    $memcache->set('postxyz_123_views',$view_count, 0, 3600);
}
//increment the views
$memcache->increment('postxyz_123_views');

$view_count = $memcache->get('postxyz_123_views');
</code>
</pre>
<p>All you need to do then is run a cron job every hour and have it dump the view counts to the database.</p>
<p>That is basically all there is to using memcached. It is very simple. If you feel adventurous, check out the <a href="http://us.php.net/manual/en/ref.memcache.php">php documentation</a> for some more advanced features.</p>
]]></content:encoded>
			<wfw:commentRss>http://dawnerd.com/post/6_using-memcached-to-reduce-database-load/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
