当memcached命中时,如何获取caches_action设置过期标头? [英] How to get caches_action to set expires headers when there is a hit in memcached?
问题描述
我有一个缓存的动作
caches_action :my_action, :expires_in=>1.hours
,并使用
def my_action
...
expires_in 1.hours
send_data(...,:disposition => 'inline',:type => 'image/png',:filename => params[:title]+".png")
end
但是,当我查看由于memcached命中而产生的结果中的缓存控制响应标头时,我得到了:
However, when I look at the cache control response header from a result that is coming as the result of a memcached hit, I get this:
Cache-Control: private, max-age=0, must-revalidate
第一次回合,即当缓存中没有任何内容时,这就是我所期望的,即:
The first time round, i.e. when there is nothing in the cache, it is what I expect, i.e.:
Cache-Control: max-age=3600, private
看起来rails + memcached既没有缓存原始的响应头,也没有设置适当的头.结果是,即使结果(图像)没有变化,客户端也会每次都向服务器发出请求.尽管该操作在缓存中被命中后很快就完成了,但它仍然结束了再次发送所有数据的过程,我想避免这种情况.
It looks like rails+memcached is neither caching the original response headers, nor setting appropriate headers itself. The result is that the client makes a request to the server each time even when the result (an image) hasn't changed. Though the action completes quickly as it gets a hit in the cache, it still winds up sending all the data again, which I'd like to avoid.
我如何获取标头以执行正确的操作,以便客户端要么首先不发出请求,要么获得未修改"响应?
How do I get the headers to do the right thing so that the client either makes no request in the first place, or gets a 'not modified' response?
推荐答案
几天前,我遇到了同样的问题.调试看来,rails并未考虑caches_action的expires_in.
I had the very same problem a few days ago. Debugging it seems that rails is not taking into acount the expires_in for caches_action.
我发现有效的方法是将相同内容放入cache_path中.例如喜欢这样做
What I found that works is to put the same inside a cache_path. For example insted of doing
caches_action :monthly_sales_by_category, :expires_in => 10.minutes, :cache_path => proc { |c|
category = c.params[:category]
{:cat => category}
}
我所做的是以下
caches_action :monthly_sales_by_category, :cache_path => proc { |c|
expires_in 10.minutes, :public => false
category = c.params[:category]
{:cat => category}
}
并且像护身符一样工作.:)
And works like a charm. :)
这篇关于当memcached命中时,如何获取caches_action设置过期标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!