【问题标题】:Caching static resources returned via VirtualPathProvider缓存通过 VirtualPathProvider 返回的静态资源
【发布时间】:2012-12-14 16:49:58
【问题描述】:

我正在使用VirtualPathProvider,它当前将虚拟路径映射到解决方案外部的目录。我建立这个主要是为了自我锻炼。这完全等同于在解决方案的目录中有一个软链接或 NTFS 硬链接。

无论如何,我设法使用我的自定义提供程序从该虚拟目录成功加载静态图像。

现在的问题是浏览器不会缓存图像。而且服务器甚至不会考虑返回缓存信息(如 ETag)。

这是我所做的:

  • GetFile(path).Open() 通过 File.Open() 返回 FileStream
  • 我确实没有覆盖GetCacheKeyGetCacheDependencies
  • 我确实覆盖了 GetFileHash 返回 Murmur 哈希(似乎是最快的,甚至比 CRC-32)并对其进行了测试
  • 调试时,GetFileHash 从未在我的提供程序中调用

CTRL-F5ing 仅返回以下标头(不引用缓存)

Cache-Control   private
Content-Length  476
Content-Type    image/png
Date    Sat, 29 Dec 2012 21:25:54 GMT
Server  Microsoft-IIS/8.0
X-AspNet-Version    4.0.30319
X-Powered-By    ASP.NET
X-SourceFiles   [...]

我目前正在 Visual Studio 的调试服务器和配备 Firebug 的 Firefox 中进行调试。

例如,这是我所期望的 (http://i.stack.imgur.com/3mn3d.png)

Accept-Ranges   bytes
Cache-Control   max-age=315360000
Content-Length  1059
Content-Type    image/png
Date    Sat, 29 Dec 2012 21:35:29 GMT
Etag    "7d636a8ef932ed081c16ace6f87b16e6"
Expires Fri, 12 Feb 2038 09:58:39 GMT
Last-Modified   Tue, 14 Feb 2012 22:07:18 GMT
Server  ECAcc (fcn/4089)
X-Cache HIT

问题很明显:如何让浏览器重新加载这些静态资源?

【问题讨论】:

  • 您能解释一下您对缓存问题的解决方案吗?

标签: asp.net caching virtualpathprovider


【解决方案1】:

为了缓存数据,我通常以这种方式使用 webconfig,这在我个人的建议下非常简单:

<system.webServer>
    <staticContent>
       <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="180.00:00:00" />
    </staticContent>
   <caching>
    <profiles>
    <add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".html" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".htm" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".pdf" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".bmp" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="DontCache" />
    </profiles>
   </caching>
  </system.webServer>

我已经解决了我所有的问题。

您可以在这里查看http://italiancallcenter.com 使用相同的技术或http://annunciando.biz,您可以在最后使用firebug 或chrome 进行检查......

我唯一没有优化过的是 etags。

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2013-02-10
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    相关资源
    最近更新 更多