#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 4 years ago on Caching with Dalli :

Man, your really cool!

chabgood said about 3 years ago on Caching with Dalli :

Would touch: true accomplish the same task? 

kobaltz PRO said about 3 years ago on Caching with Dalli :

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