Rails 4.0 expire_fragment/cache expiration not working

青春壹個敷衍的年華 提交于 2019-11-27 12:04:53

I believe the issue is that when you cache the fragment in your view, a cache digest is being added to the cache key (views/all_available_releases/41cb0a928326986f35f41c52bb3d8352), but expire_fragment is not using the digest (views/all_available_releases).

If you add skip_digest: true to the cache call in the view it should prevent the digest from being used.

<% cache "all_available_releases", skip_digest: true do %>
 <% @releases.each do |release| %>
  <% cache(release) do %>
   <html code with>
   <%ruby code @release.name blah blah blah%>
  <%end%>
 <%end%>
<%end%>

Cache digests are only intended to be used with automatic cache expiration. If you need to manually expire cache keys then you can't use cache digests.

Jbuilder don't support the skip_digest. After way to many failed approaches, I decided to share my answers here as it's highly related although not with a rails view as is the issue above.

Here's a related Q/issue where DHH essentially tells the guy that he can't expire fragment_caches explicitly. https://github.com/rails/cache_digests/issues/35 Everything isn't square so here's a way around this:

class MenuController
  def index
    json = Rails.cache.fetch('clients') do
      @items = Menu.all
      render_to_string( template: 'menu/index', locals: {items: @items})
    end
    render json: json
  end
end

then you can explictly expire this anywhere, like in an observer

class MenuCacheObserver < ActiveRecord::Observer
  observe :menu, :menuitem, :menusubnavigation

  def after_save obj
    Rails.cache.delete(:clients)
  end
end

In a few cases this may make sense. On a general note, in most cases you should be using the object in the cache input, like json.cache! @my_object do wrapping the jbuilder view. That way it would invalidate when updated_at on the object changes.

Just ran into this issue myself and the way I approached dealing with it was through regular expressions. It might not be the most elegant solution but it works fine.

ActionController::Base.new.expire_fragment(%r{offer_#{@offer.id}/*})

Adding the skip_digest is much nicer though.

In Rails 5 I took the following steps to bust the cache without resorting to skip_digest: true. Our problem was that changing the value of I18n strings does not reflect in the computed cache digest so the cache would not get busted automatically.

Here is the view where the cache block is defined:

/ views/layouts/_footer.html.slim
- cache :footer do
  span= t('shared.footer')

Then in rails console I run:

fragment = ActionController::Base.new.view_context.cache_fragment_name(:footer, virtual_path: 'layouts/_footer.html.slim')
ActionController::Base.new.expire_fragment(fragment)

cache_fragment_name will figure out the digest based on the virtual_path keyword argument.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!