Skip to content

Using Dalli with Rails

Matthew Hively edited this page Dec 6, 2023 · 9 revisions

In a Rails application Dalli can be used in three different ways:

  • Cache Store
  • Session Store
  • Direct Use of a Dalli Client

Cache Store

The most common use of Dalli in Rails is to support a cache store. The ActiveSupport::Cache::MemCacheStore, included in Rails, uses the Dalli gem to access memcached. To make use of this functionality you'll need to include dalli in your application's Gemfile, and then configure the use of a cache store in the config/environments/RAILS_ENV.rb file for each Rails environment where you want to support caching.

# Configure the cache store.  In this case, configure it to use a MemCacheStore, which uses Dalli. 
config.cache_store = :mem_cache_store, 'localhost:11211', 'abc.example.com:11211', { pool_size: 10, pool_timeout: 5 }
# Enable fragment and page caching in ActionController - optional, assuming the application wants to use page fragment caching
config.action_controller.perform_caching = true

When using the CacheStore session store, it's recommended that it be configured with a connection pool. This requires that the application include connection_pool in its Gemfile, and that one of the two options in pool: { size:, timeout: } is specified in the configuration.

Note that other Dalli::Client options are not forwarded to the instance that rails creates. To learn about the available options see the ActiveSupport::Cache::MemCacheStore guide.

⚠️ Using a Dalli::Client instance to initialize MemCacheStore is Deprecated, cause is an issue with key truncation. ⚠️

Caching API

The Rails caching API is directly accessible via Rails.cache. You don't need to use the Dalli API directly:

Rails.cache.fetch('some-data', expires_in: 1.hour) do
  SomeBigModel.long_query
end

Please read Caching with Rails for in-depth guide to caching with Rails.

Session Store

Dalli can also be used to support a Rails session store. In this implementation, only the session id is stored in the cookie sent to the user, and all of the session data set by the application is store in Memcached. This is a less common use case, and generally is only required if the default cookie session store doesn't meet your application's needs.

Please note that when using the Session Store you will need to include the connection_pool gem in your application's Gemfile. The connection_pool gem is not required to use Dalli in general, but is explicitly used by Rack::Session::Dalli.

Session store configuration is done either in the configuration block in the config/application.rb or in a dedicated initializer (e.g. config/initializers/session_store.rb. The blocks below are what you would write inside the configuration block in the config/application.rb. If you're defining the configuration in an initializer, you'll need to prepend the name of the class defined in config/application.rb (e.g. YourApp::Application.)

There are two ways to use Dalli in a Rails session store. The first is to use the ActionDispatch::Session::MemCacheStore. An example configuration would be:

# Use a `ActionDispatch::Session::MemCacheStore` for session storage
config.session_store :mem_cache_store, memcache_server: 'localhost:11211', pool_size: 10, pool_timeout: 5, expire_after: 1.day

The memcache_server parameter is required, and must be a valid value for a Dalli::Client servers configuration argument.

When using the MemCacheStore session store, using a connection pool is required. One of either the pool_size or pool_timeout options must be specified in the configuration.

Finally, the expire_after parameter controls session lifetime.

Alternately, if the application has configured a cache store as described above, the cache store can be used as a session store. In this case the configuration would look like:

# Use a `ActionDispatch::Session::CacheStore` for session storage
config.session_store :cache_store, expire_after: 1.day

In this case there's no need to specify any kind of Dalli configuration, as that's been done as part of the cache store configuration.

Direct Use of a Dalli Client

A Rails application can also use the Dalli::Client class directly, just as a non-Rails application would. In this case the application can directly instantiate Dalli::Client instances, and use the Dalli::Client API.

It's worth noting that, just as in the cases above, use of a connection pool is recommended. Applications that use a connection pool for Dalli access will need to include the connection_pool gem in the Gemfile. Please consult the connection_pool gem documentation for details.