5

I'm using a VirtualPathProvider that currently maps a virtual path to a directory external to the solution. I'm building this mainly for self-exercise. This is totally equivalent of having a soft-link or NTFS hard-link in the solution's directory.

Anyway, I managed to successfully load static images from that virtual directory using my custom provider.

Now the problem is that the browser won't cache images. And the server doesn't even think about returning cache information (like ETag).

Here is what I've done:

  • GetFile(path).Open() returns FileStream via File.Open()
  • I did not override GetCacheKey and GetCacheDependencies
  • I did override GetFileHash returning the Murmur hash (seems to be the fastest, even than CRC-32) and tested it
  • While debugging, GetFileHash is never invoked in my provider

CTRL-F5ing only returns the following headers (no reference to cache)

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   [...]

I'm currently debugging in Visual Studio's debug server and Firefox equipped with Firebug.

As an example, here is what I expected (https://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

Question is obvious: how can I get the browser not to reload these static resources?

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305

1 Answers1

0

To cache data i usually use webconfig in this manner which is really simple at my personal advice:

<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>

And i have solved all my issues.

you can take a look here http://italiancallcenter.com which use same techique or http://annunciando.biz and you can check at your end in firebug or chrome....

The only thing that i never optimized are the etags.

I hope that is is helpfull

makemoney2010
  • 1,222
  • 10
  • 11