如何为单个cucumber场景启用Rails缓存

发布于 2024-11-04 20:14:15 字数 1019 浏览 2 评论 0原文

我编写了一个 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>

有人知道如何做到这一点吗?

I've written a cron job to update some cached values in the background (rather than doing it while a user waits). I have a scenario that runs that cron job and then reads the cache to see if the values are set correctly.

The problem is that Rails.cache.read("key") is returning "Cache read: key\n" not the value, and if I debug and inspect Rails.cache I get a ActiveSupport::Cache::BlackHoleStore returned - not a good sign.

This makes sense of course because the following is included in my cucumber.env:

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

What I want is to override this at runtime - we have about a thousand scenarios and this is the only one that needs the cache active. I tried the following from the debugger prompt without any luck:

(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>

Anyone know how to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福%小乖 2024-11-11 20:14:23

试试这个...使用 @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

Try this...tag your scenarios with @enable_caching and add these hooks to your support/env.rb file:

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

After '@enable_caching' do
  ActionController::Base.perform_caching = false
end
离不开的别离 2024-11-11 20:14:23

好吧,我最终弄清楚了这一点。

简短的答案是 Rails.cacheRAILS_CACHE 的别名,虽然没有可以分配给 Rails.cache= 方法RAILS_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" |

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

Ok, so I eventually got to the bottom of this.

The short answer is Rails.cache is an alias for RAILS_CACHE, and while there's no Rails.cache= method you can assign to RAILS_CACHE, e.g.:

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

You could probably also do RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_cache) but that seems a bit dirtier to me.

The longer answer is to use a step to wrap a table of other steps with code that enables and then disables the cache, to ensure you don't break other tests by leaving caching enabled - this kind of mistake is nasty to track down.

This would look something like:

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

And using it would look something like:

 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" |

The steps in the table have obviously been implemented elsewhere.

怪异←思 2024-11-11 20:14:23

这似乎与我的问题/答案此处有关。问题是Rails 初始化后无法更改cache_store。有一个缓存初始值设定项在 Rails 初始化期间运行。据我所知,初始化后就无法更改它。也许你可以模拟缓存读/写?

This seems kind of related to my question/anser here. The issue is that you can't change cache_store after Rails is initialized. There's a cache initializer that gets run during Rails initialization. As far as I'm aware, you can't change it after it's initialized. Perhaps you can mock the cache read/write?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文