DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > memcache on Heroku's cedar stack in Rails 3.1

memcache on Heroku's cedar stack in Rails 3.1

Jeff Dickey user avatar by
Jeff Dickey
·
Jan. 07, 12 · Cloud Zone · Interview
Like (0)
Save
Tweet
3.96K Views

Join the DZone community and get the full member experience.

Join For Free

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

<span class="comment"># Gemfile</span>
gem <span class="string"><span class="delimiter">'</span><span class="content">dalli</span><span class="delimiter">'</span></span>

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

Now start caching things!

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://jeffdickey.info/memcache-on-heroku-cedar-stack-in-rails-3-1

IT Cache (computing) Session (web analytics) Host (Unix) IPv6 Command (computing) GEM (desktop environment) dev

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Blocking Ads on Your Network Using Raspberry Pi 3 + Fedora + Pi-hole
  • Java Microservices: Code Examples, Tutorials, and More
  • SQL GROUP BY and Functional Dependencies: a Very Useful Feature
  • SQL Database Schema: Beginner’s Guide (With Examples)

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo