【问题标题】:Rails.cache.fetch is feeding / fetching cache in spite of ActionController::Base.perform_caching = false尽管 ActionController::Base.perform_caching = false,Rails.cache.fetch 仍在提供/获取缓存
【发布时间】:2015-06-30 04:31:32
【问题描述】:

我想测试我的缓存行为,但类似的事情失败了:

  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 选项禁用(我使用钩子)]

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

config.cache_store = :null_store

我的 action 方法使用 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

【问题讨论】:

  • 我试图通过最后一次编辑让我的问题更容易理解,我希望这很有用:-)

标签: ruby-on-rails caching


【解决方案1】:

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

我提交了一个issue+PR 来更改可能非常misleading 的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2011-10-25
    • 1970-01-01
    相关资源
    最近更新 更多