【问题标题】:ASPX That Returns An Image - Output Cache-able?返回图像的 ASPX - 可输出缓存?
【发布时间】:2010-10-19 05:34:18
【问题描述】:

您好!

我创建了一个 APSX Web 表单,它根据一些提供的参数返回一个远程图像。可以这样使用:

<img src="/ImageGetter.aspx?param1=abc&param2=123" />

ImageGetter.aspx 的标记和代码如下所示:

<%@ OutputCache Duration="100000" VaryByParam="*" Location="ServerAndClient" %>
<%@ Page Language="C#" AutoEventWireup="false" EnableSessionState="False" CodeBehind="ImageGetter.aspx.cs" Inherits="ACME.Helpers.ImageGetter" %>

此代码在 ImageGetter.aspx 的 Page_Load 方法中调用:

byte[] data = null;
Dictionary<string, string> file_locations = GetImageLocations(param1, param2);
try
{
    data = new WebClient().DownloadData(file_locations["main"]);
}
catch (WebException wex)
{
    try
    {
        data = new WebClient().DownloadData(file_locations["backup"]);
    }
    catch (Exception e)
    {
        throw;
    }
}
Response.ContentType = "image/jpeg";
Response.OutputStream.Write(data, 0, data.Length);
Response.End();

根据我的测试,它似乎没有缓存。这可能与输出缓存有关,还是我应该根据查询字符串参数编写自己的缓存来存储字节数组?

【问题讨论】:

    标签: c# asp.net caching outputcache


    【解决方案1】:

    尝试删除 Response.End(),因为这会过早终止线程并阻止发生输出缓存。

    见:http://bytes.com/groups/net-asp/323363-cache-varybyparam-doesnt-work

    可能希望考虑使用 ASHX 处理程序并使用您自己的缓存方法。

    【讨论】:

    • Dropping Response.End 不知何故减慢了它的速度。
    • ...因为它现在正在跳过缓存环?
    【解决方案2】:

    使用 ASHX 通用处理程序并使用 HttpRuntimeCache(缓存对象)来完成 Codebrain 所说的工作。它会更快,更灵活。

    【讨论】:

      【解决方案3】:

      您的问题可能是 bug in IE - 如果使用了 Vary:* HTTP 响应标头,则它无法缓存,但 IIS 默认返回它,因为它在 HTTP 1.1 规范中。

      尝试将以下内容添加到您的 web.config:

      <system.web> 
          <caching>
              <outputCache omitVaryStar="true" />
          </caching>
      </system.web> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-06
        • 2011-02-02
        • 2012-09-21
        • 1970-01-01
        相关资源
        最近更新 更多