18-Mar-2011
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.new('127.0.0.1:11211')
=> #<Dalli::Client:0x00000104989248 @servers="127.0.0.1:11211", @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.new('127.0.0.1:11211')
=> #<Dalli::Client:0x00000104989248 @servers="127.0.0.1:11211", @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 version
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.