在ASP.NET中使用的图像文件缓存问题 [英] Image caching issue by using files in ASP.NET

查看:121
本文介绍了在ASP.NET中使用的图像文件缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET中使用文件面临着图像高速缓存的问题。我详细的解决方案是:

I am facing a problem with Image caching by using files in ASP.NET. My detailed solution is:


  • 有关要求1:我检查映像文件不存在,并且将映像文件写入到硬盘。之后,返回文件的网址到浏览器

  • For 1st request: I check the image file not exists and write the image file into hard disk. After that returns the url of the file to the browser

有关请求后:函数仅返回文件的URL到浏览器

For later requests: the function just returns the url of the file to the browser

我的问题是:有时与1请求,浏览器无法从URL中的图像数据,并导致404 - 找不到文件错误。我想可能因为文件被写入但不准备从浏览器获取,但在code,我同时使用使用说明和Dispose方法位图和放大器;图形对象,以确保一切都准备好从浏览器访问。

My problem is: sometimes with 1st requests, the browser can not get the image data from the url and cause the 404 - File Not Found error. I think it might because of the file is written but not ready for retrieving from browsers, but in the code I am using both "using statement" and "Dispose method" for bitmap & graphic objects to ensure everything are ready for accessing from browsers.

在更多的细节,我现在用的是类似的解决方案在NopCommerce:

In more details, I am using the similar solution as in NopCommerce:

lock (s_lock) { ...........
 using (var stream = new MemoryStream(LoadPictureBinary(picture))) {
  using(var b = new Bitmap(stream)){
   using(var newBitMap = new Bitmap(newSize.Width, newSize.Height)){
    .......................
    newBitMap.Save(
        Path.Combine(this.LocalThumbImagePath, localFilename),  ici, ep);
   }
  }
 } 
} // end lock

return url;

更多信息:你可以检查在<一个完整的code href=\"http://nopcommerce.$c$cplex.com/SourceControl/changeset/view/9125b9a7fafe#src%2fLibraries%2fNop.Services%2fMedia%2fPictureService.cs\" rel=\"nofollow\">http://nopcommerce.$c$cplex.com/SourceControl/changeset/view/9125b9a7fafe#src%2fLibraries%2fNop.Services%2fMedia%2fPictureService.cs (请搜索GetPictureUrl()函数)

More information: you can check the full code at http://nopcommerce.codeplex.com/SourceControl/changeset/view/9125b9a7fafe#src%2fLibraries%2fNop.Services%2fMedia%2fPictureService.cs (please search the GetPictureUrl() function)

推荐答案

这是我看的时候多发生要求共同为同一图像的情况。

This is a case that I have see when multi-request happens together for the same image.

有关这种情况下,锁是不够的,你需要使用互斥锁定此过程中所有线程和所有的请求在一起。

For this case the lock is not enough, you need to use mutex to lock this procedure for all threads and all request together.

所以,我的建议是要改变 锁定互斥和使用该图像的名称,或所述图像的code作为名称为互斥作任何锁定,并且将要由互斥的名称定义制成,但只有一个的任何图像。所以,改变与互斥锁,然后看看这个问题仍然存在。

So what I suggest is to change the lock, with mutex and use the image name, or a code of the image as name for the mutex to not lock anything and any image that is going to made but only the one that is defined by the name on the mutex. So change the lock with mutex, and then see if this problem still exist.

一个提示:互斥体名称对尺寸和人物一定的局限性。如果您使用的图像文件名作为名称的互斥体确保由互斥接受。

one tip: The mutex name have some limitations on the size and the characters. If you use the image file name as the name for the mutex be sure that is accepted by mutex.

这篇关于在ASP.NET中使用的图像文件缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆