Reputation: 183429
We need a Rails caching solution that works with a multi-dyno formation on Heroku. Specifically, we need worker dynos processing long-running tasks to write to a cache that our web dynos can read from.
Apparently the only way to implement a shared cache across dynos is by using Memcached. However I'm having trouble finding objective instructions on how to get this done. (The Heroku docs are written by Memcached add-on vendors like MemCachier that are promoting their product.)
My questions:
Upvotes: 10
Views: 2855
Reputation: 183429
(@ItamarHaber answered the question and sold me on memcached cloud. Just wanted to show exactly how we implemented it)
Using Memcached Cloud add-on with Rails 4 : (derived from instructions for Rails 3)
Add Dalli to your Gemfile:
gem 'dalli'
Set the cache_store in config/environments/production.rb:
# NOTE: ENV vars aren't available during slug comiplation, so must check if they exist:
if ENV["MEMCACHEDCLOUD_SERVERS"]
config.cache_store = :mem_cache_store, ENV["MEMCACHEDCLOUD_SERVERS"].split(','), { :username => ENV["MEMCACHEDCLOUD_USERNAME"], :password => ENV["MEMCACHEDCLOUD_PASSWORD"] }
end
UPDATE:
After a little more research we realized that Redis could provide us with all the benefits of Memcached caching plus a slew of other features. Redis Labs, the makers of the Memcached Cloud add on, also offer the Redis Cloud add on, which is just as easy to use:
Using Redis Cloud add-on with Rails 4 :
Add Redis to your Gemfile:
gem 'redis-rails'
Set the cache_store in config/environments/production.rb:
# NOTE: ENV vars aren't available during slug comiplation, so must check if they exist:
if ENV["REDISCLOUD_URL"]
config.cache_store = :redis_store, ENV["REDISCLOUD_URL"], { expires_in: 90.minutes }
end
Upvotes: 5
Reputation: 49932
Due diligence - I work at Redis Labs, the company that provides the Memcached Cloud addon.
Upvotes: 6