如何手动刷新模型缓存
我可以缓存带有片段的页面,
并在外部佣金任务中刷新它
但我不知道如何刷新控制器内的缓存以从DB中获取数据。
如何在Rake文件中刷新缓存
def get_max_and_min_routes(airline_name, from, to)
Rails.cache.fetch("I_AM_CACHE_KEY") do
FETCH_FROM_DB
end
end
action_controller = ActionController::Base.new
action_controller.expire_fragment("body_header")
action_controller.expire_fragment("index")
action_controller.expire_fragment("welcome_index_controller")
action_controller.expire_fragment("footer")
- cache("index", skip_digest: true) do
= render "historical_prices"
= render "common/recently_changed_prices"
答案 0 :(得分:0)
您可以通过Rails.cache
命令直接访问缓存,如下所示:
> Rails.cache.read('body_header')
=> '<p>fragment cache entry</p>'
> Rails.cache.delete('body_header')
=> true
> Rails.cache.read('body_header')
=> nil
所以对你的例子来说:
def get_max_and_min_routes(airline_name, from, to)
content_to_be_cached = FETCH_FROM_DB
Rails.cache.write('body_header', content_to_be_cached)
end
这应该在你的rake文件和控制台中都有效。