如何使控制台中的视图缓存片段到期?

就像是

Rails.cache.delete('site_search_form') 

似乎没有用。 这可能吗? 谢谢。

使用与使用Rails.cache访问的密钥略有不同的密钥创建缓存片段条目。

使用expire_fragment (您可以将其发送到控制器): http : expire_fragment

ActionController的:: Base.new.expire_fragment(钥匙)

 Rails.cache.delete "views/site_search_form" 

在Rails 5中,我采取了以下步骤来破坏缓存而不使用skip_digest: true 。 我们的问题是更改I18n字符串的值不会反映在计算的缓存摘要中,因此缓存不会自动被破坏。

以下是定义缓存块的视图:

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

然后在rails控制台中运行:

 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将根据virtual_path关键字参数确定摘要。