<?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>Papermashup.com &#187; Memcache</title>
	<atom:link href="http://papermashup.com/category/memcache/feed/" rel="self" type="application/rss+xml" />
	<link>http://papermashup.com</link>
	<description>Ashley Ford :: CSS &#124; PHP &#124; JavaScript</description>
	<lastBuildDate>Wed, 07 Jul 2010 09:39:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Memcache With PHP</title>
		<link>http://papermashup.com/using-memcache-with-php/</link>
		<comments>http://papermashup.com/using-memcache-with-php/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 21:18:14 +0000</pubDate>
		<dc:creator>Ashley</dc:creator>
				<category><![CDATA[API's]]></category>
		<category><![CDATA[Memcache]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Social Networks]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Tools]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[headline]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://papermashup.com/?p=1231</guid>
		<description><![CDATA[I want to just start this little post with that fact that I donâ€™t know all the ins and outs of using Memcache but I have enough knowledge to just about get it working. I have access to a Mediatemple Dedicated Virtual Extreme server and managed to install it on the server using SSH, with the help of this little guide from Mediatemple which runs you through the installation process step by step.
Who uses Memcache?
Well Memcache was developed by Danga Interactive to enhance the speed ...]]></description>
			<content:encoded><![CDATA[<p>I want to just start this little post with that fact that I donâ€™t know all the ins and outs of using Memcache but I have enough knowledge to just about get it working. I have access to a Mediatemple Dedicated Virtual Extreme server and managed to install it on the server using SSH, with the help of this little guide from Mediatemple which runs you through the installation process step by step.</p>
<h3>Who uses Memcache?</h3>
<p>Well Memcache was developed by Danga Interactive to enhance the speed of LiveJournal.com, Memcache dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a Memcache miss. Currently FaceBook are the biggest users of Memcache. Infact FaceBook are using it so aggressively that they are chartering new territory, and helping to develop the open source project.</p>
<p><img src="http://papermashup.com/wp-content/uploads/2009/11/fb.png" alt="fb" title="fb" width="582" height="200" class="alignnone size-full wp-image-1221" /></p>
<h3>Whatâ€™s Memcached?</h3>
<p>So your reading this wondering what Iâ€™m talking about. Memcache is basically is a general-purpose distributed memory caching system, put that in English, it allows you to store any form of data in a â€˜temporary cacheâ€™ so wherever you go to do a database query, instead of just connecting to the database and getting the data we want we first check the memcache to see if our data is already stored. If the memcache returns nothing, then go to the database, get what youâ€™re looking for, then store it in the memcache for later:</p>
<p>There are five main functions that we use with Memcache and they are as follows:</p>
<ul>
<li>get() â€” gets the value for a specified key</li>
<li>set() â€” sets a given key with a given value</li>
<li>add() â€” adds to the cache, only if it doesnâ€™t already exist</li>
<li>replace() â€” sets in the cache only if the key already exists</li>
<li>flush() â€” removes all keys and cached data</li>
</ul>
<p><img src="http://papermashup.com/wp-content/uploads/2009/11/mem1.png" alt="mem1" title="mem1" width="582" height="87" class="alignnone size-full wp-image-1215" /></p>
<h3>The Code</h3>
<p>So once you have Memcache installed on your server you can connect to it and start caching stuff. Itâ€™s worth pointing out that you shouldnâ€™t go out and cache everything, itâ€™s really only useful when you have large amounts of data that are going to be requested regularly.</p>
<ol>
<li>So the first two lines setup the connection to Memcache.</li>
<li>The include is just my database connection script.</li>
<li>Next we set the key. When we store data using Memcache there are 3 parts to storing the data, those three parts are the key, the value, and the expiry time of the cached item. The key is used to reference the stored data. in this example you can see that the key is an MD5 hash of the database query.</li>
<li>we then check if the key exists in the cache. this will return true or false. if the data is found in the cache then we can access it.</li>
<li>If the data isn&#8217;t found in the cache we connect to the database to get it. we store the data in the cache by using the follow line:  $memcache->set($key, $row, TRUE, 20); $row refers to the array that we get from the database, notice the 20 at the end. This refers to the how long this item will expire in the cache. It is measured in seconds. </li>
</ol>
<pre class="brush: php;">

$memcache = new Memcache;
$memcache-&amp;gt;connect('127.0.0.1', 11211) or die (&amp;quot;Could not connect&amp;quot;);

include('includes/connect.php');

//set the key then check the cache
$key = md5(&amp;quot;SELECT * FROM memcached_test where name='ashley'&amp;quot;);
$get_result = $memcache-&amp;gt;get($key);
if ($get_result) {
echo $get_result['name'];
echo $get_result['username'];
echo &amp;quot;Data Pulled From Cache&amp;quot;;
}
else {
 // Run the query and get the data from the database then cache it
 $query=&amp;quot;SELECT * FROM memcached_test where name='ashley';&amp;quot;;
 $result = mysql_query($query);
 $row = mysql_fetch_array($result);
 print_r($row);
 $memcache-&amp;gt;set($key, $row, TRUE, 20); // Store the result of the query for 20 seconds
 echo &amp;quot;Data Pulled from the Database&amp;quot;;
}
</pre>
<p>This is a very simple example of how to use memcache, but I hope it has been an insight into how caching works. </p>
]]></content:encoded>
			<wfw:commentRss>http://papermashup.com/using-memcache-with-php/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>
