Rails 3 片段缓存输出问题
在使用 memcached 查看 Rails 3.0.4 应用程序中的片段级缓存时,我遇到了一个问题。我对正在发生的事情有点困惑,但我认为这与从缓存区域内提取输出的方式有关。我在 -vv 模式下本地运行 memcached,并且可以看到正确保存/拉取片段的密钥,问题是 memcached 中项目的值。
这就是我正在做的事情:
< ... html before ... >
<%= cache("item_#{i.id}") do %>
<%= render :partial => 'shared/item', :locals => { :item => i, :functionality => [:set_as_default] } %>
<% end %>
< ... html after ... >
当我查看缓存中键的值时,它具有来自该片段缓存块中的页面内的 html,但也位于该页面之外(来自之前的 html 和之后的 html地区)。不过,这是有趣的部分,这就是我认为它与捕获输出相关的原因——它不处理整个页面,只处理之前和之后的一些 html。
根据rails片段缓存指南,我认为我做得正确(http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching)。有人知道会发生什么吗?
非常感谢您的帮助!
-埃里克
I am running into an issue when looking into fragment-level caching within my Rails 3.0.4 application with memcached. I am a bit confused with what is going on, but I think it's something to do with the way the output is being pulled from within the caching region. I am running memcached locally in -vv mode, and can see the key for the fragment getting saved/pulled correctly, the problem is the value of the item within memcached.
Here is what I'm doing:
< ... html before ... >
<%= cache("item_#{i.id}") do %>
<%= render :partial => 'shared/item', :locals => { :item => i, :functionality => [:set_as_default] } %>
<% end %>
< ... html after ... >
When I look at the value of the key within the cache, it has html from within the page that is in that fragment cache block, but ALSO OUTSIDE of that (from both the html before and html after areas). Here is the interesting part though, and is kind of the reason I think its related to capturing the output--it doesn't do the whole page, only some of the html before and some after.
According to the rails fragment cacheing guide, I think I'm doing things correctly (http://guides.rubyonrails.org/caching_with_rails.html#fragment-caching). Does anyone have thoughts as to what could be going on?
Your help is much appreciated!
-Eric
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,您错误地使用了 ERB。基本上去掉=号。您所做的就是返回块的值,因此您会看到双输出。
<% 缓存("item_#{i.id}") do %>
此外,ActiveRecord 对象响应内部烘焙的 #cache_key 方法。尝试利用这一点。 ActiveRecord 对象的默认 #cache_key 也使用类名称、对象 id 和 Updated_at 时间戳。缓存方法应该能够接受多个参数或一个数组,并且它将依次为响应它的每个对象调用cache_key。使用这种方法,这意味着当对象更新时你将缓存未命中,非常酷的东西。因此,IIRC
<%cache("item",i) do %>
In this case, you are using ERB incorrectly. Basically take out the = sign. What your doing is your returning the value of the block too and hence why you are seeing double output.
<% cache("item_#{i.id}") do %>
Also, ActiveRecord objects respond to an internally baked in #cache_key method. Try to take advantage of that. The default #cache_key for an ActiveRecord object uses the class name, the object id and the updated_at timestamp too. The cache method should be able to take multiple args or an array and it will inturn call cache_key for every object that responds to it. Using this method, it means you will cache miss when the object is updated to, pretty cool stuff. So, IIRC
<% cache("item",i) do %>