cachingruby-on-rails-3.2sweeper

non-deterministic behavior of fragment_cache_key


I've got the following method in a sweeper:

def expire_topic_articles_cache_for topic_id
  key = fragment_cache_key :controller => :articles, :action => :index, :topic_id => topic_id
  Rails.logger.debug "Expire article list cache for topic #{topic_id}"
  Rails.logger.debug "   using key #{key}"
  # use RegExp due to the fragment covering multiple pages
  expire_fragment %r{#{key}}
end

The problem is with calling fragment_cache_key. It returns the correct key in one code path, but it returns nil in another code path, both times called with the same topic_id, and both times triggered by the same controller but from different observed models.

This makes no sense to me. I expect fragment_cache_key to return the same result for the same input. It should be side-effect free given the same cache configuration is used (and that's the same in both code pathes).


Solution

  • Sigh... After digging around here for a while I found this answer:

    https://stackoverflow.com/a/2275001/124536

    That took me on the right path: The controller did not configure the cache_sweeper for this particular action which triggered the sweeper. Strange enough the sweeper was triggered anyway but just not working correctly.