从HTTPRuntime缓存中检索图像

时间:2012-06-07 05:37:14

标签: c# asp.net asp.net-cache

我正在尝试从HTTPRuntime Cache保存并检索图像,但我得到一个例外。我能够将流保存到缓存中,但是当我尝试检索它时,我得到一个异常说:

  

请求已中止。连接意外关闭

这是我的代码:

public void ProcessRequest(HttpContext context)
{   
    string courseKey = context.Request.QueryString["ck"];
    string objKey = context.Request.QueryString["file"];

    if(HttpRuntime.Cache[objKey] !=null)
    {
        using (Stream stream = (Stream)HttpRuntime.Cache[objKey]) // here is where I get an exception
        {
            var buffer = new byte[8000];
            var bytesRead = -1;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, bytesRead);
            }
        }
        return;
    }
    var response = Gets3Response(objKey, courseKey, context);           

    if (response != null)
    {
        using (response)
        {
            var MIMEtype = response.ContentType;
            context.Response.ContentType = MIMEtype;
            var cacheControl = context.Response.CacheControl;
            HttpRuntime.Cache.Insert(objKey, response.ResponseStream, null, DateTime.UtcNow.AddMinutes(20), Cache.NoSlidingExpiration);
            using (Stream responseStream = response.ResponseStream)
            {
                var buffer = new byte[8000];
                var bytesRead = -1;
                while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                   context.Response.OutputStream.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}

这是我得到的例外

Exception

1 个答案:

答案 0 :(得分:3)

这段代码非常令人困惑。首先,您正在缓存response.ResponseStream,但它已包含在using块中。因此,当您到达HttpRuntime.Cache.Insert时,response.ResponseStream已经被释放并关闭。因此错误。

您不应该缓存流。首先,一旦你放置了分布式缓存服务,你的方法将是不可能的。你需要重构这个。考虑:

public class CacheAsset
{
   public string FileName { get; set; }
   public string ContentType { get; set; }
   public byte[] Content { get; set; }
}

CacheAsset GetAsset(HttpContext context)
{
   string courseKey = context.Request.QueryString["ck"];
   string objKey = context.Request.QueryString["file"];

   var asset = context.Cache[objKey] as CacheAsset;

   if (asset != null) return asset;

   using (var response = Gets3Response(objKey, courseKey, context))
   using (var stream = new MemoryStream())
   { 
      var buffer = new byte[8000];
      var read = 0;

      while ((read = response.ReponseStream.Read(buffer, 0, buffer.Length)) > 0)
      {
         stream.Write(buffer, 0, read);
      }

      asset = new CacheAsset
              {
                 FileName = objKey,
                 ContentType = reponse.ContentType,
                 Content = stream.ToArray()
              };
       context.Cache.Insert(objKey, asset, null, DateTime.UtcNow.AddMinutes(20), Cache.NoSlidingExpiration);
   }

   return asset;
}

public void ProcessRequest(HttpContext context)
{
   var asset = GetAsset(context);

   context.Response.ContentType = asset.ContentType;
   context.Response.BinaryWrite(asset.Content);
}