在我的一个观点中,我有:
- cache("crumb") do
%ul.breadcrumb
在浏览器中,视图会加载,您会看到面包屑。
在development.rb
中,我有:
config.consider_all_requests_local = true
config.action_controller.perform_caching = true
config.cache_store = :dalli_store
加载视图后,为什么我会:
ActionController::Base.new.read_fragment("crumb")
Cache read: views/crumb
D, [2016-01-14T12:14:12.707228 #2465] DEBUG – : Cache read: views/crumb
Read fragment views/crumb (0.5ms)
I, [2016-01-14T12:14:12.707727 #2465] INFO – : Read fragment views/crumb (0.5ms)
=> nil
缓存可以从命令行读取和写入:
ActionController::Base.new.write_fragment("crumb", "foo")
Cache write: views/crumb
D, [2016-01-14T12:14:29.401736 #2465] DEBUG – : Cache write: views/crumb
Write fragment views/crumb (1.2ms)
I, [2016-01-14T12:14:29.402987 #2465] INFO – : Write fragment views/crumb (1.2ms)
=> "foo"
[4] pry(main)> ActionController::Base.new.read_fragment("crumb")
Cache read: views/crumb
D, [2016-01-14T12:14:33.990289 #2465] DEBUG – : Cache read: views/crumb
Read fragment views/crumb (0.6ms)
I, [2016-01-14T12:14:33.990858 #2465] INFO – : Read fragment views/crumb (0.6ms)
=> "foo"
但是即使我以这种方式在命令行写入缓存后,当我重新加载页面时,我在浏览器中看到了我的视图的HTML,而不是“foo”。
注意:一旦我开始工作,我将在模板中包含模型对象。但是为了这篇文章的目的,我故意简化了测试用例。
答案 0 :(得分:0)
答案是缓存实际上是有效的,但要在控制台中测试它,您需要包含Rails自动附加的缓存摘要。您可以从日志中复制粘贴此摘要。您最终输入类似Rails.cache.read("views/crumb/etcetc...")
/