I have a dynamically generated CSS file. It's fairly large, and has different content for different browsers. It changes very rarely.
So at the top of the .aspx
page I have:
<%@ OutputCache Duration="86400"
Location="ServerAndClient"
VaryByParam="none"
VaryByCustom="browser" %>
I have a similar directive against MVC actions that generate dynamic images:
[OutputCache(Duration = 86400,
VaryByParam = "none",
Location = OutputCacheLocation.ServerAndClient)]
This should result in the file being cached on the server and the client (but not intermediate proxies) for a day.
The response headers appear to be correct:
HTTP/1.1 200 OK
Cache-Control: private, max-age=83831
Content-Type: text/css; charset=utf-8
Expires: Wed, 09 Jun 2010 08:52:45 GMT
Last-Modified: Tue, 08 Jun 2010 08:52:45 GMT
Vary: *
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Tue, 08 Jun 2010 09:35:34 GMT
Content-Length: 70073
The server side caching works - further requests do not execute the code again.
However the client side caching is broken in two different ways: firstly the content is requested again on every page, despite the fact that it should be cached.
Why does the browser request the content again?
Secondly when the server is asked for the content again it returns an HTTP 200 along with the entire content. It should return an HTTP 304 Not Modified with no body - telling the browser to re-use what it has already downloaded.
How do I make sure that it returns an HTTP 304 when the content hasn't changed?
The bottom line is that any request you make for an ASPX resource will always result in an immediate new fetch from the server as if the page was never cached.
Use the SetNoStore() method as follows, in the ASPX page: <%@ Page Language=”C#” %> <% Response. Cache. SetNoStore(); Response.
To manually cache application data, you can use the MemoryCache class in ASP.NET. ASP.NET also supports output caching, which stores the generated output of pages, controls, and HTTP responses in memory. You can configure output caching declaratively in an ASP.NET Web page or by using settings in the Web. config file.
Configure Output Caching Through the IIS ManagerFrom the Start menu, click Administrative Tools, and then click Internet Information Services (IIS) Manager. In the tree view on the left side, find your application. Select the Output Caching menu item. In the right column, click Add in the Action menu.
1) Vary: * will cause certain browsers, such as IE, not to cache. Vary: User-Agent might work better (not sure).
2) WRT 200 vs 304: does the client send an If-* header?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With