开发者

Selectively cache .js and .png files over https?

We are creating a large secure back office web application in ASP.NET. All access to the site is over https connections, and we'd like to either turn off caching for pages or set caches to expire quickly.

However, the site uses quite a few images and largish javascript files/libraries. Is there a way to selectively cache certain f开发者_C百科iles or file types so they are not being reloaded all the time?


Please look into headers of the sort:

  • Expires: Mon, 23 Nov 2009 07:30:00 GMT
  • Cache-Control: public, max-age=86400

You should read, or skim, Caching in HTTP.

With ASP.NET MVC, you will want to adorn your controller actions with the OutputCacheAttribute. For example:

[OutputCache(Duration = 24*60*60)] // Cached for 1 day
public ActionResult Index() { return View(); }

In ASP.NET you can use the OutputCache directive in your aspx file:

<%@ OutputCache Duration="86400" %>


After some additional Googling, it looks like I might just need to create a web.config in my Scripts and Images folders (and any others I'd like to cache):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
     <staticContent>
        <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
     </staticContent>
  </system.webServer>
</configuration>

Does this seem right? Did I miss something?


You need to use the "far future expire" pattern. Without using output cache, here's how you can do it. (Personally I think this looks nicer than output caching.)

HttpContext.Response.ExpiresAbsolute = DateTime.Now.AddYears(5);

Of course you can change the DateTime to whatever you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜