如何执行检查是否启用了Rails 5缓存?

mai*_*her 2 caching ruby-on-rails ruby-on-rails-5

在Rails 5中,可以启用/禁用缓存:

rails dev:cache
=> Development mode is now being cached.

rails dev:cache
=> Development mode is no longer being cached.
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果启用或禁用了缓存,是否有一种方法可以使应用程序从中获取信息?就像是:Rails.cache.enabled?

我知道可以检查file的存在tmp/caching-dev.txt,但是我正在寻找更高级别的东西。

Hie*_*ham 6

启用缓存后,rails config中的cache_store不得为:null_store,因此我们可以轻松地通过以下方式进行检查:

Rails.application.config.cache_store != :null_store
Run Code Online (Sandbox Code Playgroud)

=> true表示启用缓存

或者我们可以直接用perform_caching标志检查:(谢谢@AjinkyaPisal)

Rails.application.config.action_controller.perform_caching
Run Code Online (Sandbox Code Playgroud)

  • 我认为最好使用`config.action_controller.perform_caching` http://edgeguides.rubyonrails.org/caching_with_rails.html (3认同)