Getting memcached working with rails3 on Mac OS


Setting up rails 3 to use memcached is really simple using the dalli gem.

Add the dalli gem in your Gemfile

gem 'dalli'

Configure your production.rb (and development.rb, etc.) to use the dalli_store cache store

config.cache_store = :dalli_store, 'localhost:11211'

Install memcached

sudo port install memcached

Start memcached

memcached -vv

You should see something like:

slab class   1: chunk size    104 perslab 10082
slab class   2: chunk size    136 perslab  7710
slab class  38: chunk size 458992 perslab     2
<4 server listening

Great, you're ready to go.

Now start the rails console and test it out

rails c

What you want to see is:

> dc ='')
=> #<Dalli::Client:0x00000104989248 @servers="", @options={:expires_in=>0}> 

> dc.set("test","something")
 => true

> dc.get("test")
 => "something" 

Great, it's working!

Not working?

What if you actually get:

> dc ='')
=> #<Dalli::Client:0x00000104989248 @servers="", @options={:expires_in=>0}> 

> dc.set("test","something")
 => false

Check your memcached log output. Perhaps you see this?

<8 new client connection
<8 connection closed.

dalli on rails 3 has problems running with versions of memcached less than 1.4, so first check that you've got a recent version of memcached running.

telnet localhost 11211

If you get something like VERSION 1.2.8 then you have a problem... sudo port install memcached should have installed the newest version of memcached, so something in your mac configuration is calling up an old version of memcached that doesn't work with dalli.

One easy (but perhaps not ideal) solution is simply to add this line to your .bash_profile

alias memcached=/opt/local/bin/memcached

Then restart memcached from within a new command window and it should now be running the latest version.

| |


Add your comment