我正在尝试实现 Web 服务调用的客户端缓存,并且基于来自 Web 的信息,我能够根据SetCachingPolicy()函数来完成它,如下面的代码 1所示。
我能够成功地使用 Web 方法 RetrieveX,但不能使用方法 RetrieveY。我注意到 RetrieveX 没有参数,而 RetrieveY 有一个字符串参数,在 Fiddler 下检查时,区别似乎是来自 RetrieveY 的 Web 服务调用的 HTTP GET 请求具有参数的查询字符串。
到目前为止,所有没有查询字符串的 HTTP GET Web 服务调用都在正确地进行缓存,但这个调用中没有查询字符串。
在 Fiddler 下检查表明 RetrieveX 在输出 1中具有以下缓存信息,而 RetrieveY 在输出 2中具有信息。
这是这种缓存方法的限制,还是我可以做些什么来让 RetrieveY 的客户端缓存工作?
代码 1:SetCachingPolicy
private void SetCachingPolicy()
{
HttpCachePolicy cache = HttpContext.Current.Response.Cache;
cache.SetCacheability(HttpCacheability.Private);
cache.SetExpires(DateTime.Now.AddSeconds((double)30));
FieldInfo maxAgeField = cache.GetType().GetField(
"_maxAge", BindingFlags.Instance | BindingFlags.NonPublic);
maxAgeField.SetValue(cache, new TimeSpan(0, 0, 30));
}
代码 2:RetrieveX
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public string[] RetrieveX()
{
SetCachingPolicy();
// Implementation details here.
return array;
}
代码 3:检索
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public string[] RetrieveY(string arg1)
{
SetCachingPolicy();
// Implementation details here.
return array;
}
输出 1:RetrieveX 缓存信息
HTTP/200 responses are cacheable by default, unless Expires, Pragma, or Cache-Control headers are present and forbid caching.
HTTP/1.0 Expires Header is present: Wed, 12 Sep 2012 03:16:50 GMT
HTTP/1.1 Cache-Control Header is present: private, max-age=30
private: This response MUST NOT be cached by a shared cache.
max-age: This resource will expire in .5 minutes. [30 sec]
输出 2:检索缓存信息
HTTP/200 responses are cacheable by default, unless Expires, Pragma, or Cache-Control headers are present and forbid caching.
HTTP/1.0 Expires Header is present: -1
Legacy Pragma Header is present: no-cache
HTTP/1.1 Cache-Control Header is present: no-cache
no-cache: This response MUST NOT be reused without successful revalidation with the origin server.