在ExtLib REST控件中禁用缓存(使用dojox.data.JsonRestStore) [英] Disable cache in ExtLib REST control (which uses dojox.data.JsonRestStore)
问题描述
在我的XPage中,我有一个 xe:djxDataGrid
( dojox.grid.datagrid
),它使用 xe:restService
似乎使用 dojox.data.JsonRestStore
。
一切正常,没有代理,但我的客户端通过代理访问应用程序由于公司政策。用户更新DataGrid后的数据时,它会在代理服务器后面显示旧值。
当REST Control / JsonRestStore发送一个ajax GET请求以获取数据时,有请求头中没有 Cache-Control
参数。而Domino不会在回调标头中放置 Expires
参数。我相信这就是为什么旧版本的GET请求被代理缓存。
我们已经尝试在浏览器中禁用缓存,但这并不意味着代理是缓解请求。
我相信这可以通过以下方式解决:
- 在请求标头中设置
Cache-Control
参数或 - 设置
过期
参数回复标题
但是我还没有找到一种方法来设置其中之一。对于XPage Domino集合过期:-1
响应头,但不是用于ajax GET请求,它是:
/mypage.xsp /?$$ viewid =!ddrg6o7q1z!& $$ axtarget = view:_id1:_id2:callback1:restService1
将JSON数据返回给JsonRestStore并由代理缓存。
一个选项是尝试得到一个例外的代理,所以请求到这个网站将绕过代理缓存。但是例外情况一般不容易得到。
任何想法?谢谢。
Update1
我的同事建议我可以拦截xhr GET由dojox.data.JsonRestStore发出的请求,并添加一个时间参数到URL以防止缓存。这是我的问题:
Update2
@SvenHasselbach有一个很好的解决方案来防止所有xhrs缓存:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache -prevention-for-dojo-xhr-requests
似乎工作完美,& dojo.preventCache =
参数被添加到URL中,并且请求似乎也返回正确的JSON也带有这个参数。 但是当我使用该代码时,DataGrid停止工作。每个xhr都会导致此错误:
尝试使用Firefox和Chrome。数据的第一页仍然加载,因为xhr拦截还没有到位,但后续页面在每个单元格中只显示...。
解决方案是 Sven Hasselbach 的 Julian Buss博客的评论部分,需要稍加修改。
我将 xhrPost
更改为 xhrGet
并没有将代码放在 dojo.addOnLoad
中。放置在DataGrid / Store上的第一个XHR中无效。
我还删除了头
修改,因为它覆盖现有的头。当REST控件从具有 xhrGet
的服务器请求数据时,URL始终是相同的,请求的行在HTTP头中,如下所示:
范围:items = 0-9
(和其他)标题在使用原始代码时消失。要添加标题,我们将把现有的标题从 args
中附加到它们。我没有看到需要,因为它应该足以在URL中添加参数。这是我使用的非常简单的代码:
if(!(dojo._xhrGet)){
dojo ._xhrGet = dojo.xhrGet;
}
dojo.xhrGet = function(args){
args ['preventCache'] = true;
return dojo._xhrGet(args);
}
现在我得到所有行,所有XHR获取URL都有& dojo.preventCache =
参数正是我想要的。接下来,我们将在客户环境中测试,看看是否解决了他们的问题。
更新
p Jul Jul Jul cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache cache
更新
客户报告它正在为他们工作!
In my XPage I have a xe:djxDataGrid
(dojox.grid.datagrid
) which uses xe:restService
which seems to use dojox.data.JsonRestStore
.
Everything works fine without proxy but my client accesses the application via a proxy because of corporate policy. After a user updates data in the DataGrid it shows old values when accessed behind the proxy.
When the REST Control/JsonRestStore sends an ajax GET request to get data, there is no Cache-Control
parameter in request headers. And Domino does not place Expires
parameter in the reponse headers. I believe that's why the old version of the GET request gets cached by the proxy.
We have tried to disable cache in browsers but that does not help which indicates the proxy is caching the requests.
I believe this could be solved either by:
- Setting
Cache-Control
parameter in request headers OR - Setting
Expires
parameter in response headers
But I haven't found a way to set either of these. For the XPage Domino sets Expires:-1
response header but not for the ajax GET request which is:
/mypage.xsp/?$$viewid=!ddrg6o7q1z!&$$axtarget=view:_id1:_id2:callback1:restService1
This returns the JSON data to JsonRestStore and gets cached by the proxy.
One options is to try to get an exception to the proxy so requests to this site would bypass the proxy cache. But exceptions are generally not easy to get thru.
Any ideas? Thanks.
Update1
My colleque suggested that I could intercept the xhr GET requests made by dojox.data.JsonRestStore and add a time parameter to the URL to prevent cache. Here is my question about that:
Prevent cache in every Dojo xhr request on page
Update2
@SvenHasselbach has a great solution for preventing cache for all xhrs:
http://openntf.org/XSnippets.nsf/snippet.xsp?id=cache-prevention-for-dojo-xhr-requests
It seems to work perfectly, &dojo.preventCache=
parameter is added to the URLs and the requests seem to return correct JSON also with this parameter. But the DataGrid stops working when I use that code. Every xhr causes this error:
Tried with Firefox and Chrome. The first page of data still loads because xhr interception is not yet in place but the subsequent pages show only "..." in each cell.
The solution is Sven Hasselbach's code in the comment section of Julian Buss's blog which needs to be slightly modified.
I changed xhrPost
to xhrGet
and did not place the code to dojo.addOnLoad
. When placed there it was not effective in the first XHR by the DataGrid/Store.
I also removed the headers
modification because it overrides existing headers. When the REST control requests data from server with xhrGet
the URL is always the same and rows requested are in HTTP header like this:
Range: items=0-9
This (and other) headers disappear when the original code is used. To just add headers we would have take the existing headers from args
and append to them. I didn't see a need for that because it should be enough to add the parameter in the URL. Here is the extremely simple code I'm using:
if( !(dojo._xhrGet )) {
dojo._xhrGet = dojo.xhrGet;
}
dojo.xhrGet = function (args) {
args['preventCache'] = true;
return dojo._xhrGet(args);
}
Now I'm getting all rows and all XHR Get URLs have &dojo.preventCache=
parameter which is exactly what I wanted. Next we'll test in customer environment to see if this solves their problem.
Update
As Julian points out in his blog I could also use a Web Site Rule to set Expires
or cache-control
http response headers.
Update
The customer reports it's working now for them!
这篇关于在ExtLib REST控件中禁用缓存(使用dojox.data.JsonRestStore)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!