Rack Cache on Heroku with Memcached

by Jim Gay

memcache-add-on The convenience of deploying application on Heroku is attractive especially with their add-ons and the free options that they provide, in particular Memcache.

If you’re working with an application which needs to manage it’s cache with Rack::Cache, you’ll want to have fast responses for your metastore. The meta information about your content is probably the most important part of your cache since it’s checked by many clients for the status of the content: is it fresh or stale? See more about How Web Caches Work.

Typically you might setup Rack::Cache like this:

config.middleware.use Rack::Cache, :metastore => 'file:tmp/cache/meta', :entitystore => 'file:tmp/cache/entity'

5MB of Memcache is a decent place to start for free and it’s integrated into your application without any effort on your part. So on Heroku you can use Memcache as your metastore like this:

$cache = Memcache.new
config.middleware.use Rack::Cache, :metastore => $cache, :entitystore => 'file:tmp/cache/entity'

That’s simple enough, and it’s just as easy if you’re deploying Radiant:

$cache = Memcache.new
config.middleware.use Radiant::Cache, :metastore => $cache

If you want to look at an example of a simple app that does this, there’s an easy to understand sample application on github. Enjoy your speedy metastore.

Comments

Tj Singleton said on Thursday, June 24, 2010:

What is the benefit of Rack::Cache in conjunction with varnish that heroku already provides?

Jim Gay said on Thursday, June 24, 2010:

You can control Rack::Cache from your app if it has a backend in Memcache. With Varnish, your only control is the headers you set when the content is generated. After that, you need to wait for the Varnish cache to expire.

John W. Long said on Thursday, June 24, 2010:

Jim, I dropped this in my 0.9 app, but get an uninitialized constant “Memcache” error. When I changed it to “Memcached” it seemed to work, but I still get some kind of error:

config.gem ‘memcached’
require ‘memcached’
$cache = Memcache.new
config.middleware.use Radiant::Cache, :metastore => $cache, :entitystore => $cache

What am I doing wrong?

Jim Gay said on Thursday, June 24, 2010:

John, be sure to read the docs on heroku

But do you have the add-on installed? And have you set the cache_store with:

config.cache_store = :mem_cache_store, Memcached::Rails.new

Stuart Henry said on Thursday, June 24, 2010:

What is the difference between using Rack::Cache and the :mem_cache_store, as in (in production.rb)

config.cache_store = :mem_cache_store, Memcached::Rails.new

Tj Singleton said on Thursday, June 24, 2010:

Ok, I get it. I see in your sample app your removing the cache-control headers. Does this have a negative effect on browser caching? It won’t be receiving the max-age directive.

Ryan Johnson said on Tuesday, July 06, 2010:

John, I just got it working.

You'll need to do this for running radiant on heroku:

environment.rb
config.gem 'memcached'
require 'memcached'

production.rb
config.cache_store = :mem_cache_store, Memcached::Rails.new

config.middleware.use ::Radiant::Cache, :metastore => "memcached://#{ENV['MEMCACHE_SERVERS']}:11211/meta", :entitystore => "memcached://#{ENV['MEMCACHE_SERVERS']}:11211/body"

"Reference":http://rtomayko.github.com/rack-cache/storage.html

Ryan Daigle said on Tuesday, August 09, 2011:

To see this working on Cedar you can view my demo app here: https://github.com/rwdaigle/demo-cedar-rackcache

1999 - 2014 © Saturn Flyer LLC 2321 S. Buchanan St. Arlington, VA 22206

Call Jim Gay at 571 403 0338