Caching模塊用來實現(xiàn)caches_action/caches_page/fragment,普通用法沒發(fā)現(xiàn)什么問題,問題在于使用下面的例子時,它不能緩存到正確的位置:
class?CacheController?<?ApplicationController
??caches_action?:cache_one
??caches_page???:cache_two
??#
end class?TestController?<?ApplicationController
??def?one
????render?:inline?=>?<<-EOS
Test::one<br?/>
<%=?render_component?:controller?=>?'cache',?:action?=>?'cache_one'?%>
????EOS
??end
??def?two
????render?:inline?=>?<<-EOS
Test::two<br?/>
<%=?render_component?:controller?=>?'cache',?:action?=>?'cache_two'?%>
????EOS
??end
??#
end cache_one和cache_two的實現(xiàn)就省略了。
訪問直接訪問這2個action,能夠在正確的目錄下生成緩存文件。不過訪問test/one和test/two就會出現(xiàn)緩存錯誤,2個緩存文件被生成到CACHE_ROOT/test下,試想如果在多個地方使用render_component,就會產(chǎn)生很多份一樣內(nèi)容的緩存。
request會被傳遞給controller,當訪問/test/one時,這個request保留有這個url。調(diào)用render_component時,會生成一個新的controller,這個request也被傳遞,不過url卻還是/test/one,所以就有這個問題。
修復:
module?ActionController?#:nodoc:
??module?Caching
????module?Actions
??????class?ActionCacheFilter?#:nodoc:
????????def?before(controller)
??????????return?unless?@actions.include?(controller.action_name.intern)
??????????url?=?controller.url_for(:controller?=>?controller.controller_name,?:action?=>?controller.action_name)
??????????if?cache?=?controller.read_fragment(url.split("://").last)
????????????controller.rendered_action_cache?=?true
????????????controller.send(:render_text,?cache)
????????????false
??????????end
????????end
????????def?after(controller)
??????????return?if?!@actions.include?(controller.action_name.intern)?||?controller.rendered_action_cache
??????????url?=?controller.url_for(:controller?=>?controller.controller_name,?:action?=>?controller.action_name)
??????????controller.write_fragment(url.split("://").last,?controller.response.body)
????????end
??????end
????end
??end
end
通過替換controller和action來解決,測試結果正確。
不過還有其它問題沒修復,比如url中參數(shù)的問題,雖然調(diào)用render_component時我并沒有傳遞參數(shù),但它還是把參數(shù)給傳遞了。另外沒有修復caches_page和fragment。
其實這個問題是由render_component帶來的,所以最好的修復辦法當然是修復這個方法,讓它處理正確的controller/action/params,暫時還沒找出修復方法。