#18 Caching with Dalli

Summary

Dalli is a great interface for Memcached through Ruby.
rails performance caching 6:46

Resources

Summary

Be sure to check out the `memcached` documentation for setting up your server to allocate the correct amount of memory to your `memcached` service.

# Gemfile
gem 'dalli'

# development.rb
config.action_controller.perform_caching = true

# development.rb & production.rb
config.cache_store = :dalli_store, 'localhost', { :namespace => 'dev-018' }

# user.rb
    def self.cached_find(id)
      Rails.cache.fetch(['user', id], expires_in: 5.minutes) { find(id) }
    end

    after_commit :flush_cache

    private

    def flush_cache
      Rails.cache.delete(['user', id])
    end

# users_controller.rb
    @user = User.cached_find(params[:id])