高速缓存问题,将两个连续的HTTP GET请求从APP1运行到APP2 [英] Cache problem running two consecutive HTTP GET requests from an APP1 to an APP2
问题描述
我使用Ruby on Rails 3,并有两个应用程序(APP1和APP2)在两个子域上工作:
$ b
我试着运行两个连续的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 |
如果< 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> }
结束
结束
结束
在第一个请求I得到正确的XML(response1就是我所期望的),但是第二个不是(response2不是我所期望的)。做了一些测试后,我发现第二次发现< model_name> .find(params [:id])。< field_name>
run(对于elsif语句)总是一个空白值,以便elseif语句中的代码永远不会运行。
有可能这个问题与缓存有关< model_name> ; .find(params [:id])。< field_name>
?
如果我尝试使用这段代码
respond_to do | format |
如果< 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是整个< model_name> .find(params [:id]
object请注意,我删除了elsif条件(为了运行第二个请求,以便我可以得到response2),并且我改变了代码从
format.xml {render:xml => < model_name> .find(params [:id])}
至
format.xml {render:xml =>< model_name> .find(params [:id])。< field_name>}
PS:我读了 eTag和Conditional GET ,但我不确定我必须使用这种方法,我想保持简单。
有可能将查询缓存在应用程序控制器级别。Rails在行为结束时销毁查询缓存离子,但您的查询不在行动中。
值得一提的是APP2中的代码很可能不应该在应用程序控制器中,而是在控制器。这是更加RESTful,并且可能还会照顾缓存问题(因为Rails将清除APP1请求之间的查询缓存)。
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.
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).
这篇关于高速缓存问题,将两个连续的HTTP GET请求从APP1运行到APP2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!