How do I expire a view cached fragment from console?

前端 未结 4 886
耶瑟儿~
耶瑟儿~ 2020-12-28 16:53

Something like

Rails.cache.delete(\'site_search_form\')

doesn\'t seem to work. Is this possible? Thanks.

相关标签:
4条回答
  • 2020-12-28 17:38
    Rails.cache.delete "views/site_search_form"
    
    0 讨论(0)
  • 2020-12-28 17:39

    ActionController::Base.new.expire_fragment(key)

    0 讨论(0)
  • 2020-12-28 17:40

    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

    0 讨论(0)
  • 2020-12-28 17:53

    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.

    0 讨论(0)
提交回复
热议问题