加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 当请求具有查询字符串时,Web服务的缓存不起作用

发布时间:2020-12-16 09:37:31 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试实现Web服务调用的客户端缓存,并且基于来自Web的信息,我能够根据SetCachingPolicy()函数执行它,如下面的代码1所示. 我能够成功地使用Web方法RetrieveX,但不能使用方法RetrieveY.我注意到RetrieveX没有参数,RetrieveY有一个字符串参数,在Fiddler下
我正在尝试实现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,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,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.

解决方法

我也遇到过这个问题,我想我会分享对我有用的东西.根本问题是没有在响应上设置VaryByParams.如果将其添加到SetCachingPolicy()方法中,RetrieveY应该按预期开始工作:

cache.VaryByParams["*"] = true

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读