Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use Memcache on Heroku's Cedar Stack in Rails 3.1

DZone's Guide to

How to Use Memcache on Heroku's Cedar Stack in Rails 3.1

· Cloud Zone
Free Resource

MongoDB Atlas is a database as a service that makes it easy to deploy, manage, and scale MongoDB. So you can focus on innovation, not operations. Brought to you in partnership with MongoDB.

If you're building a Rails site, you almost definitely need to have caching. memcache is the best solution for that problem right now. It's not too hard, but there are only bits on the internet on how to do it.

Instructions

Install memcache addon

$ heroku addons:add memcache

 

Install memcache locally

$ sudo port install memcached         # for macports
$ brew install memcached              # for homebrew
$ sudo apt-get install memcached      # for ubuntu/debian

 

Put the 'dalli' gem into your Gemfile (this is the memcache driver) and run the 'bundle' command

# Gemfile
gem 'dalli'

 

In production.rb and development.rb, set the cache_store

# /config/environments/production.rb
config.cache_store = :dalli_store

 

# /config/environments/development.rb
config.cache_store = :dalli_store

Now change your session initializer (for memcache session support)

# config/initializers/session_store.rb
require 'action_dispatch/middleware/session/dalli_store'
YOURAPPNAMEHERE::Application.config.session_store :dalli_store

 

How this works

This works without setting any configuration since dalli will connect to the MEMCACHE_SERVERS environment variable, which gets set when you install the addon. If that environment variable is not present, it will connect to localhost (which works for development).

Dev environment troubleshooting

The most common error you will see is dalli not being able to connect. First, make sure that memcache is running. For that, I like to use telnet to connect to the local running memcache service. It's kinda fun too!

$ telnet 127.0.0.1 11211

 

Then you can do:

SET foo bar
GET foo

 

(should return bar)

Still running into problems?

I've ran into a problem on Vagrant and a Windows development installs where Dalli would not be able to find the local running memcache service. For that, I found that setting the host to '127.0.0.1' fixed the problem. I imagine it's something to do with IPv6, but I'm not sure.

So to fix, just do

$ export MEMCACHE_SERVERS=127.0.0.1

 

That worked for me!

 

Source: http://dickey.xxx/memcache-on-heroku-cedar-stack-in-rails-3-1

MongoDB Atlas is the best way to run MongoDB on AWS — highly secure by default, highly available, and fully elastic. Get started free. Brought to you in partnership with MongoDB.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}