通过VirtualPathProvider返回静态资源的缓存

5
我正在使用一个VirtualPathProvider,它将虚拟路径映射到解决方案外部的目录。我主要是为了自己练手而构建这个。这完全相当于在解决方案目录中有一个软链接或NTFS硬链接。
无论如何,我成功地使用我的自定义提供程序从那个虚拟目录加载静态图像。
现在的问题是浏览器不会缓存图像。服务器甚至不考虑返回缓存信息(例如ETag)。
这是我做过的:
- GetFile(path).Open() 通过 File.Open() 返回 FileStream - 我没有重写 GetCacheKey 和 GetCacheDependencies - 我确实重写了 GetFileHash,返回 Murmur 哈希(似乎比 CRC-32 更快),并进行了测试 - 在调试时,GetFileHash 从未在我的提供程序中被调用
CTRL-F5 只返回以下标头(没有缓存引用)
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

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

请问您能否解释一下缓存问题的解决方案? - Cristian E.
1个回答

0

为了缓存数据,我通常使用Web.config文件,这种方式非常简单,是我的个人建议:

<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中检查一下...

我唯一没有优化的是etag。

希望这有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接