Steps To Follow to Use Dalli as your cache Store
1. Dalli depends on the memcache hence install memcache in you machine using the following command
sudo apt-get install memcached
2. Add Dalli gem to your gem file
gem ‘dalli’
3. Add the below configuration to your rails development.rb
# Memcached
config.perform_caching = true
config.action_controller.perform_caching = true
config.cache_store = :dalli_store, ‘localhost:11211′
4. Dalli set up has been completed.
5. Now time to move on to caching in the controller and view level
In controller you can create a cache key using Rails.cache.fetch() which create if the key doesn’t exist.In Case key exist it fetch the key.
Eg: @product_category = Rails.cache.fetch(“product_category”, :expires_in => 1.hour) do
ProductCategory.find params[:category]
end
The above code create a key name product_category and store the value which is found. The second param expires_in its a memcache feature which tell that cache destroy time.In our case it destroys in 1hour.
6.View level caching Fragment caching (technically)
<% cache(‘navigation_bar_header’, :expires_in => 15.minutes) do %>
<%= navigation_menu %>
<%end%>
the above code behaves same way as the cache do in controller,the key ‘navigation_bar_header’ hold the values for 15 min.