【问题标题】:Output cache not working for AJAX varyByParam输出缓存不适用于 AJAX varyByParam
【发布时间】:2013-10-08 12:41:18
【问题描述】:

我有一个 WCF Web 服务的端点,它没有在 AJAX 请求的性能监视器中获得输出缓存命中,它确实获得了对端点的 AJAX 请求的输出缓存。

网络配置:

<caching>
  <outputCacheSettings >
    <outputCacheProfiles>
      <add location="Any" name="myCache" duration="3600" varyByParam="*" />
     </outputCacheProfiles>
  </outputCacheSettings>
</caching>

配置中是否缺少我的选项?我没有包含 Javascript,因为我怀疑问题出在哪里,因为服务器不应该检查任何标头来确定缓存的价值。

端点:

[AspNetCacheProfileAttribute("myCache")]
        [WebGet(ResponseFormat= WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "suburb?name={name}&state={state}&category={category}&bedrooms={bedrooms}&country={country}")]

【问题讨论】:

  • 您也可以发布 Javascript 吗?我刚刚尝试重新创建它以查看是否不显式启用输出缓存行可能会产生效果但无济于事,并且只能通过 JS 修改获得您所看到的行为。

标签: c# asp.net .net wcf caching


【解决方案1】:

当您进行 ajax 调用时,您可能会将其作为 cache: false 传递。

当您这样调用时,ajax 调用将传递带有额外参数的 URL,并且值将是一个随机的唯一数字。由于 asp.net 方法接收到这个额外的参数,并且您使用 varyByParam="*" 配置 outputcache,因此该方法永远不会缓存。解决方案是

  1. 在 ajax 调用中设置 cache: true - (在调用动态方法时不好)或
  2. 设置输出缓存 varyByParam="none"(仅在您的方法需要缓存时使用,无论参数如何)或
  3. 设置输出缓存 varyByParam="parameter1;parameter2"(如果您有多个参数,这是理想的解决方案)。对于单参数使用 varyByParam="parameter1"

抱歉回复晚了。刚才看到这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2015-07-02
    • 2012-12-25
    • 2012-07-23
    • 1970-01-01
    • 2017-09-28
    相关资源
    最近更新 更多