如何为单个cucumber场景启用Rails缓存
我编写了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个...使用
@enable_caching
标记您的场景,并将这些挂钩添加到您的support/env.rb
文件中:Try this...tag your scenarios with
@enable_caching
and add these hooks to yoursupport/env.rb
file:好吧,我最终弄清楚了这一点。
简短的答案是
Rails.cache
是RAILS_CACHE
的别名,虽然没有可以分配给的
,例如:Rails.cache=
方法RAILS_CACHE您也可以这样做
RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_cache)
但这对我来说似乎有点脏。更长的答案是使用一个步骤将其他步骤的表与启用然后禁用缓存的代码一起包装,以确保您不会通过启用缓存来破坏其他测试 - 这种错误很难追踪。
这看起来像:
使用它看起来像:
表中的步骤显然已在其他地方实现。
Ok, so I eventually got to the bottom of this.
The short answer is
Rails.cache
is an alias forRAILS_CACHE
, and while there's noRails.cache=
method you can assign toRAILS_CACHE
, e.g.: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:
And using it would look something like:
The steps in the table have obviously been implemented elsewhere.
这似乎与我的问题/答案此处有关。问题是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?