Thursday, October 16, 2008

Adding Memcached to a Rails 2.1 Application in Mac OSX 10.5

I just couldn't find anything explicit on the web about this, but it turns out to be really easy.

Install libevent:

cd to libevent directory
Run: ./configure; sudo make; sudo make install;

Install memcache:

cd to memcached directory
Run: ./configure; sudo make; sudo make install;

I don't know if all the sudo's are required, but it failed do to permissions without it. I suspect it's just required on the 'make'.

The run memcache:

> memcache

It is started with defaults for the ip and port of

In my environment.rb - I added

config.cache_store = :mem_cache_store, 'localhost', ''

Then - restarted my web app and started using Rails.cache.

To use it, in the controller do something like this:

@list_of_items = Rails.cache.fetch('list_of_items', :expires_in => 60*60 ) { Item.find(:all, :conditions => ['column = ?', @value]) }

1 comment:

  1. The sudo should only be required on `make install` since that's what puts files in directories that you don't have permission to write to.