ruby 如何从控制台过期视图缓存片段?

ncgqoxb0  于 2023-06-22  发布在  Ruby
关注(0)|答案(5)|浏览(79)

就像

Rails.cache.delete('site_search_form')

好像不管用。这可能吗?谢谢

vcirk6k6

vcirk6k61#

ActionController::Base.new.expire_fragment(key)

quhf5bfb

quhf5bfb2#

创建缓存片段条目时使用的键与使用Rails.cache访问的键略有不同。
使用expire_fragment代替(您可以将其发送到控制器):http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#M000438

uemypmqf

uemypmqf3#

Rails.cache.delete "views/site_search_form"
d8tt03nd

d8tt03nd4#

在Rails5中,我采取了以下步骤来破坏该高速缓存,而没有求助于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关键字参数计算出摘要。

kninwzqo

kninwzqo5#

另一种选择,如果你使用Redis作为缓存存储,是直接使用keys方法从redis中搜索和删除。

# get direct Redis client from Rails cache store
redis = Rails.cache.redis

# search for your key
keys = redis.keys.grep(/site_search_form/)

# delete the keys
keys.each do |key|
  redis.del(key)
end

相关问题