Start by adding the dalli gem to your Gemfile. Dalli
is a Ruby memcache client.
'dalli' gem
Then run bundle install:
$ bundle install
Once this gem is installed you’ll want to configure the Rails cache_store
appropriately. Modify your config/environments/production.rb
with the following:
:mem_cache_store,
config.cache_store = ENV["MEMCACHIER_SERVERS"] || "").split(","),
(:username => ENV["MEMCACHIER_USERNAME"],
{:password => ENV["MEMCACHIER_PASSWORD"],
:failover => true,
:socket_timeout => 1.5,
:socket_failure_delay => 0.2,
:down_retry_delay => 60
}
The values for MEMCACHIER_SERVERS
, MEMCACHIER_USERNAME
, and MEMCACHIER_PASSWORD
are listed on your cache overview page. Make sure to add them to your environment.
In your development environment, Rails.cache defaults to a simple in-memory store and so it doesn’t require a running memcached.
From here you can use the following code examples to use the cache in your Rails app:
Rails.cache.write("foo", "bar")
Rails.cache.read("foo") puts
When adding the dalli gem to your Rails 2 Gemfile you will need to use dalli v1.0.5 as later versions of Dalli don’t support Rails 2.
'dalli', '~>1.0.5' gem
Also modify config/environment.rb
to contain:
'dalli' config.gem
Else proceed just as newer Rails versions.