Skip to content

Using Dalli on Heroku

Peter Goldstein edited this page Nov 15, 2021 · 1 revision

To get Dalli to use the Heroku memcache server, you need do nothing special in your configuration - Dalli will pick up the MEMCACHE_SERVERS environment variable and use those server(s).

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

# Gemfile
gem 'dalli'

Sinatra

You can also use Dalli with Sinatra, via rack-cache or independently.

require 'dalli'
set :cache, Dalli::Client.new

# use the settings convention to access your cache
settings.cache.set('color', 'blue')

Just like with Rails, Dalli will automatically pick up Heroku's MEMCACHE environment variables. However if you need to setup something specific, such as an expires_in option, here's how:

Dalli::Client.new(ENV['MEMCACHE_SERVERS'], 
    :username => ENV['MEMCACHE_USERNAME'], 
    :password => ENV['MEMCACHE_PASSWORD'], 
    :expires_in => 300)

See also

Heroku Docs on Memcache
Heroku Developer Center - Using Rack::Cache With Memcached for Static Assets in Rails 3.1+