#18 Caching with Dalli


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


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

Gemfilegem 'dalli'
development.rbconfig.action_controller.perform_caching = true
development.rb & production.rbconfig.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) }

    after_commit :flush_cache


    def flush_cache
      Rails.cache.delete(['user', id])
users_controller.rb    @user = User.cached_find(params[:id])
mabel said almost 2 years ago:

Man, your really cool!

chabgood said about 1 year ago:

Would touch: true accomplish the same task? 

kobaltz PRO said about 1 year ago:

Only if your cache key included the updated_at attribute or similar. Part of the issue is also when you have a collection of records as you might find in Russian Doll Caching. Even if the individual record's cache is expired, you will still need to expire the outer cache (parent cache) as well since it is stale at this point.    

Login to Comment