Caching with Dalli

#18 Caching with Dalli
9/22/2015

Summary

Dalli is a great interface for Memcached through Ruby.
0
rails performance caching

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.

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) }
    end

    after_commit :flush_cache

    private

    def flush_cache
      Rails.cache.delete(['user', id])
    end
users_controller.rb    @user = User.cached_find(params[:id])
901101?v=3&s=64
mabel said 9 months ago:

Man, your really cool!

60613?v=3&s=64
chabgood said 3 months ago:

Would touch: true accomplish the same task? 

635114?v=3&s=64
kobaltz said 3 months 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