How do I expire a view cached fragment from console?

别来无恙 提交于 2019-11-30 03:18:14
Terry

Cache fragment entries are created with a slightly different key than what you access with Rails.cache.

Use expire_fragment instead (you can send it to a controller): http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#M000438

ActionController::Base.new.expire_fragment(key)

Rails.cache.delete "views/site_search_form"

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.

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