尽管ActionController :: Base.perform_caching = false,Rails.cache.fetch仍在馈送/获取缓存 [英] Rails.cache.fetch is feeding / fetching cache in spite of ActionController::Base.perform_caching = false

查看:50
本文介绍了尽管ActionController :: Base.perform_caching = false,Rails.cache.fetch仍在馈送/获取缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试我的缓存行为,但以下几行失败:

I want to test my caching behavior, and something along these lines fails:

  context "without cache" do

    it "should return the appropriate result, computing it every time", :caching => false do
      p ActionController::Base.perform_caching
      Object.should_receive(:where).exactly(4).and_call_original
      result1 = SomeService.action(args1, args2)
      result2 = SomeService.action(args1, args2)
    end

  end

...即使为 ActionController :: Base.perform_caching 打印出了 false .[这被:caching =>禁用;组的false 选项(我使用钩子)]

...even though false is printed out for the ActionController::Base.perform_caching. [This is disabled by the :caching => false option of the bloc (I use a hook)]

如果我通过从测试环境的默认:memory_store 切换到:null_store 完全关闭了缓存,则测试将按预期通过.我只是在 test.rb 中添加此行:

If I totally shut down caching by switching from the default :memory_store to the :null_store for the testing environment, the test passes as expected. I just add this line in test.rb:

config.cache_store = :null_store

我的 action 方法使用Rails的核心缓存:

My action method uses the core caching from Rails:

def action(args1, args2):
...
  objects = Rails.cache.fetch(cache_key, expires_in: 5.days) do
    object.where(id: args1).where(charac: args2).includes(...).to_a
  end
...
end

推荐答案

事实上, ActionController :: Base.perform_caching 或其对应的 config.action_controller.perform_caching >旨在控制低级缓存.

As a matter of fact, ActionController::Base.perform_caching nor its counterpart config.action_controller.perform_caching is meant to control low level caching.

我提交了 issue + PR 来更改可能非常误导人.

I submitted an issue+PR to change the docs that can be very misleading.

这篇关于尽管ActionController :: Base.perform_caching = false,Rails.cache.fetch仍在馈送/获取缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆