【问题标题】:How to enable the Rails cache for a single cucumber scenario如何为单个黄瓜场景启用 Rails 缓存
【发布时间】:2011-08-17 08:57:20
【问题描述】:

我编写了一个 cron 作业来在后台更新一些缓存值(而不是在用户等待时执行)。我有一个场景运行该 cron 作业,然后读取缓存以查看值是否设置正确。

问题是 Rails.cache.read("key") 返回的是 "Cache read: key\n" 而不是值,如果我调试和检查 Rails.cache 我得到一个 ActiveSupport::Cache::BlackHoleStore 返回 - 这不是一个好兆头。

这当然是有道理的,因为我的cucumber.env 中包含以下内容:

config.action_controller.perform_caching             = false
require File.join(RAILS_ROOT, 'lib', 'black_hole_store.rb')
config.cache_store = :black_hole_store

我想要的是在运行时覆盖它——我们有大约一千个场景,这是唯一需要激活缓存的场景。我在调试器提示符下尝试了以下操作,但没有任何运气:

(rdb:1) ActionController::Base.perform_caching = true
true
(rdb:1) ActionController::Base.perform_caching
true
(rdb:1) Rails.cache
#<ActiveSupport::Cache::BlackHoleStore:0x101b6dc60 @logger_off=false>
(rdb:1) ActionController::Base.cache_store = :memory_store
:memory_store
(rdb:1) Rails.cache
#<ActiveSupport::Cache::BlackHoleStore:0x101b6dc60 @logger_off=false>

有人知道怎么做吗?

【问题讨论】:

    标签: ruby-on-rails caching cucumber cache-control


    【解决方案1】:

    好的,所以我终于明白了。

    简短的回答是Rails.cacheRAILS_CACHE 的别名,虽然没有可以分配给RAILS_CACHERails.cache= 方法,例如:

    ActionController::Base.perform_caching = true
    ActionController::Base.cache_store = :memory_store
    RAILS_CACHE = ActionController::Base.cache_store
    

    您也可以使用RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_cache),但这对我来说似乎有点脏。

    更长的答案是使用一个步骤来包装其他步骤的表格,其中包含启用然后禁用缓存的代码,以确保您不会通过启用缓存来破坏其他测试 - 这种错误很难跟踪下来。

    这看起来像:

    When /^I do the following steps with caching enabled:$/ do |table|
      old_perform_caching = ActionController::Base.perform_caching
      old_action_controller_cache = ActionController::Base.cache_store
      old_rails_cache = RAILS_CACHE
    
      ActionController::Base.perform_caching = true
      ActionController::Base.cache_store = :memory_store
      RAILS_CACHE = ActionController::Base.cache_store
    
      steps table.raw.flatten.join("\n")
    
      ActionController::Base.perform_caching = old_perform_caching
      ActionController::Base.cache_store = old_action_controller_cache
      RAILS_CACHE = old_rails_cache
    end
    

    使用它看起来像:

     When I do the following steps with caching enabled:            
     | When I run "rake cron:nightly"             |
     | Then the cache for "x" should return "6"   |
     |  And the cache for "y" should return "13"  |
     |  And the cache for "z" should return "800" |
    

    表中的步骤显然已经在其他地方实现了。

    【讨论】:

    • 另一种方法是使用@cache 标记要使用缓存运行的场景。然后,您可以实现逻辑以在场景之前启用缓存,然后在 env.rb 文件中的前块和后块中禁用它。
    【解决方案2】:

    试试这个...用 @enable_caching 标记您的场景并将这些挂钩添加到您的 support/env.rb 文件中:

    Before '@enable_caching' do
      ActionController::Base.perform_caching = true
    end
    
    After '@enable_caching' do
      ActionController::Base.perform_caching = false
    end
    

    【讨论】:

      【解决方案3】:

      这似乎与我的问题/回答 here 有关。问题是您无法在 Rails 初始化后更改 cache_store 。在 Rails 初始化期间运行缓存初始化程序。据我所知,初始化后您无法更改它。也许你可以模拟缓存读/写?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-14
        • 2019-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多