<?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>How they Discovered Something Worth Knowing &#187; Memcached</title>
	<atom:link href="http://www.silverchairsolutions.com/blog/category/memcached/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.silverchairsolutions.com/blog</link>
	<description>reflections on life, technology and brownies</description>
	<lastBuildDate>Wed, 12 May 2010 14:39:21 +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>Memcached and OSX</title>
		<link>http://www.silverchairsolutions.com/blog/2007/07/memcached-and-osx/</link>
		<comments>http://www.silverchairsolutions.com/blog/2007/07/memcached-and-osx/#comments</comments>
		<pubDate>Thu, 19 Jul 2007 11:44:04 +0000</pubDate>
		<dc:creator>mhagedorn</dc:creator>
				<category><![CDATA[Memcached]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://www.silverchairsolutions.com/blog/?p=4</guid>
		<description><![CDATA[I was working on part of my soon to be launched rails project, snowballr, and found part of one of my processes to be particular expensive from a computational perspective. Being a bright sort of chap, my immediate reaction was &#8220;cache the results&#8221; and since I come from a proud lineage of Java developers I [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on part of my soon to be launched rails project,  <a href="http://www.snoballr.com">snowballr</a>, and found part of one of my processes to be particular expensive from a computational perspective.    Being a bright sort of chap, my immediate reaction was &#8220;cache the results&#8221; and since I come from a proud lineage of Java developers I bravely set off to create my own caching scheme.  This of course turned out to be a really bad case of the &#8220;Not Invented Here&#8221; syndrome which of course my enterprisy roots have tended to foster <img src='http://www.silverchairsolutions.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .   I looked around for other solutions, and found the tried and true <strong>memcached</strong>  solution that has been used by tons of people to accomplish the same thing. <strong>Memcached</strong> (pronounced mem-cache-dee) is a distributed caching framework that alleviates database load. It was originally developed for the website LiveJournal.com.   My main reason for not considering that before, is  that it can start to seem like you need a million moving pieces of code all working together in order to get a rails application properly running (you know the drill, mongrel, pound.. rmagick &#8211;YIKES).  I just didn&#8217;t want to add to the complexity.    </p>

<p>Furthermore I was hosting at a shared host, and things like <strong>memcached</strong> just aren&#8217;t going to happen.  I had already got bitten once by assuming distributed ruby, drb would run at my shared host, and when it came to deploy time I was in for a rude awakening.   Since then I have set up a virtual server at SliceHost, and so I control whatever is on that box.  Memcached suddenly looked like a viable solution.</p>

<p>Here is what I did to set it up on my MacBook Pro.   Since OSX by default has some issues with some of the binary releases available, Geoffrey Grosenbach has written a script which downloads, patches and compiles all the right stuff.   Get the script <a href="http://www.topfunky.net/svn/shovel/memcached/install-memcached.sh">here</a>.     This script will create binaries in /usr/local/bin, so make sure that is in your path.  </p>

<p>after downloading the script, cd to a directory of your choice (I created a directory &#8220;memcached&#8221; in my development folder) and run the script.  It should happily churn while you are getting your coffee.</p>

<p>At the conclusion of the script, you need to edit your .bash<em>profile to include EVENT</em>NOKQUEUE=1.  I am not sure what voodo this does, but its required <img src='http://www.silverchairsolutions.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .  Go ahead and pull that configuration into your current environment by entering
<code>$ source  ~/.bash_profile</code></p>

<p>Next you&#8217;ll need to install the Ruby memcached client.  This allows the rails application to put and get things from the cache.  Install it with:</p>

<p><code>$ sudo gem install memcache-client</code></p>

<p>Now that the client and server is installed, you can start doing some basic testing.  To start the server type:</p>

<p><code>$ memcached -vv</code></p>

<p>( the -vv means verbose output)</p>

<p>Once the server is up and running, you have to tell your rails application how to find your distributed cache.  On both my development and production environments, my cache will be running on localhost, so my configuration won&#8217;t change when I deploy.  For that reason I will put the necessary rails configuration information in the generic <em>config/environment.rb</em>
 file rather than the more specific <em>config/environments/development.rb</em> (or <em>production.rb</em>).  Configure the memcached client by adding the following lines to  the end of environment.rb:</p>

<p><code>CACHE = MemCache.new :namespace => "my<em>application",
                     :c</em>threshold => 10000,
                     :compression => true,
                     :debug => false,
                     :readonly => false,
                     :urlencode => false</p>

<p>CACHE.servers = '127.0.0.1:11211'
ActionController::Base.session<em>options[:expires] = 1800
ActionController::Base.session</em>options[:cache] = CACHE</code></p>

<p>(you could of course have run the memcached server on any arbitrary address, and included that address  in the servers parameter shown above)</p>

<p>Now that this information is entered, you can test the basic operation of the cache from the rails console and observe inbound requests from the memcached process you started earlier.  Enter the following from the rails console:</p>

<p><code>$ script/console
Loading development environment</p>

<blockquote>
  <blockquote>
    <p>CACHE["test"] = "my cool cached info"
    =>"my cool cached info"
    CACHE["test"]
    =>"my cool cached info"
    CACHE.delete "test"
    =>"DELETED\r\n"
    CACHE["test"]
    =>nil</code></p>
  </blockquote>
</blockquote>

<p>and thats pretty much it!   To use it in your controllers or models simply put objects in the cache using CACHE[<key>] = object or CACHE[<key>] to retrieve.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.silverchairsolutions.com/blog/2007/07/memcached-and-osx/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
