从 APP1 到 APP2 运行两个连续的 HTTP GET 请求时出现缓存问题
我使用 Ruby on Rails 3,并且有 2 个应用程序(APP1 和 APP2)在两个子域上运行:
- app1.domain.local
- app2.domain.local
,我尝试运行从 APP1 到 APP2 的两个连续 HTTP GET 请求,如下所示:
代码在APP1(请求)中:
before_filter :run_request
def run_request
response1 = Net::HTTP.get( URI.parse("http://app2.domain.local?test=first&id=1") )
response2 = Net::HTTP.get( URI.parse("http://app2.domain.local/test=second&id=1") )
end
APP2(响应)中的代码:
before_filter :run_response
def run_response
respond_to do |format|
if <model_name>.find(params[:id]).<field_name> == "first"
<model_name>.find(params[:id]).update_attribute ( <field_name>, <field_value> )
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
elsif <model_name>.find(params[:id]).<field_name> == "second"
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
end
end
end
在第一个请求之后,我得到了正确的XML(response1是我所期望的),但在第二个请求中却不是(response2不是我所期望的)。做了一些测试,我发现第二次
运行(对于 elsif 语句)它总是返回一个空白值,以便elseif 语句中的代码永远不会运行。
该问题是否可能与缓存
有关?
注意
如果我尝试此代码,
respond_to do |format|
if <model_name>.find(params[:id]).<field_name> == "first"
<model_name>.find(params[:id]).update_attribute ( <field_name>, <field_value> )
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
else
format.xml { render :xml => <model_name>.find(params[:id]) }
end
end
response2 是整个
对象。请注意,我删除了“elsif”条件(为了运行第二个请求,这样我就可以得到响应2),我将代码从
format.xml { render :xml => <model_name>.find(params[:id]) }
改为
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
P.S.:我读到了 eTag 和条件 GET,但我不确定是否必须使用该方法,我想保持一切简单。
I use Ruby on Rails 3 and I have 2 applications (APP1 and APP2) working on two subdomains:
- app1.domain.local
- app2.domain.local
and I am tryng to run two consecutive HTTP GET requests from APP1 to APP2 like this:
Code in APP1 (request):
before_filter :run_request
def run_request
response1 = Net::HTTP.get( URI.parse("http://app2.domain.local?test=first&id=1") )
response2 = Net::HTTP.get( URI.parse("http://app2.domain.local/test=second&id=1") )
end
Code in APP2 (response):
before_filter :run_response
def run_response
respond_to do |format|
if <model_name>.find(params[:id]).<field_name> == "first"
<model_name>.find(params[:id]).update_attribute ( <field_name>, <field_value> )
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
elsif <model_name>.find(params[:id]).<field_name> == "second"
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
end
end
end
After the first request I get the correct XML (response1 is what I expect), but on the second it isn't (response2 isn't what I expect). Doing some tests I found that the second time that <model_name>.find(params[:id]).<field_name>
run (for the elsif statements) it returns always a blank value so that the code in the elseif statement is never run.
Is it possible that the problem is related on caching <model_name>.find(params[:id]).<field_name>
?
NOTICE
If I try this code
respond_to do |format|
if <model_name>.find(params[:id]).<field_name> == "first"
<model_name>.find(params[:id]).update_attribute ( <field_name>, <field_value> )
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
else
format.xml { render :xml => <model_name>.find(params[:id]) }
end
end
the response2 is the whole <model_name>.find(params[:id]
object. Note that I removed the "elsif" condition (in order to run the second request, so that I can get response2) and I changed the code from
format.xml { render :xml => <model_name>.find(params[:id]) }
to
format.xml { render :xml => <model_name>.find(params[:id]).<field_name> }
P.S.: I read about eTag and Conditional GET, but I am not sure that I must use that approach. I would like to keep all simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查询可能缓存在应用程序控制器级别。 Rails 在操作结束时销毁查询缓存,但您的查询不在操作中。
就其价值而言,APP2 中的代码很可能不应该位于应用程序控制器中,而应该位于控制器中的操作中。这更加 RESTful,并且可能还会解决缓存问题(因为 Rails 将清除 APP1 请求之间的查询缓存)。
It is possible the query is cached at the application controller level. Rails destroys query caches at the end of actions, but your queries are not in an action.
For what it's worth the code in APP2 most likely shouldn't be in the application controller, but in an action in the controller. That is much more RESTful, and will probably also take care of caching issues (as Rails will clear the query cache between APP1's requests).