-
Notifications
You must be signed in to change notification settings - Fork 453
Using Dalli with Rails
In a Rails application Dalli can be used in three different ways:
- Cache Store
- Session Store
- Direct Use of a Dalli Client
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 either the pool_size
or pool_timeout
options is specified in the configuration.
Note that other Dalli::Client
options can be included in the (optional) parameters hash in the cache store configuration. Please see the Dalli::Client
documentation for details.
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.
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.
Custom session store configuration is typically made in a dedicated initializer, rather than i the per-environment configuration file. The code examples below follow that format.
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
Rails.application.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, 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 either the pool_size
or pool_timeout
options is 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
Rails.application.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.
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.
-
General Information
- Requirements and Integrations
- Installing memcached
-
Getting Started
- Using Dalli with SSL/TLS
- Setting up a Development Environment
- Configuring the Dalli Client
- Operational Considerations