Dalli Memchache with Rails

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.

Ruby Constants

Variable Name Variable Value
$@ The location of latest error
$_ The string last read by gets
$. The line number last read by interpreter
$& The string last matched by regexp
$~ The last regexp match, as an array of subexpressions
$n The nth subexpression in the last match (same as $~[n])
$= The case-insensitivity flag
$/ The input record separator
$\ The output record separator
$0 The name of the ruby script file currently executing
$* The command line arguments used to invoke the script
$$ The Ruby interpreter’s process ID
$? The exit status of last executed child process