7

I'm trying to test some caching configuration, i want my page to stay in cache for 1 min before the request reaches the server again.

Using this simple test.asp page that has a link to itself:

<% Option Explicit %>
<%
    Response.Expires = 1
    Response.CacheControl = "private, max-age=60"
%>
<html>
<head><title>test</title></head>
<body>
<% =Now() %>
<br />
<a href="test.asp">test</a>
</body>
</html>

This works perfectly on my development computer http://localhost/test.asp, (clicking the link does not refresh the printed datetime during 1 min).

However it has not the desired effect when i put the page on the production server. After only few seconds of clicking the link I get a new datetime meaning the request reached the web server.

I use Chrome dev tool and see these response headers:

HTTP/1.1 200 OK
Cache-Control: private, max-age=60
Content-Type: text/html
Expires: Tue, 12 May 2015 19:16:52 GMT
Last-Modified: Tue, 12 May 2015 19:10:00 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Tue, 12 May 2015 19:15:55 GMT
Content-Length: 205

Can anyone help explain why it does not work on the prod server ?

update I tried with Chrome, Firefox and IE, and also 2 pages test.asp and test2.asp, both having a link to the other page, and got exactly the same problem, after 8-12 sec, the page refresh instead of waiting 60sec before refreshing.

Jonathan
  • 1,276
  • 10
  • 35
  • How are you testing? Refresh in same tab? Than this might be a chrome issue: http://stackoverflow.com/questions/11245767/is-chrome-ignoring-control-cache-max-age Reason that it worked on your dev environment could be that Chrome does funky things with the 'localhost' url anyway.. – AardVark71 May 13 '15 at 09:27
  • I'm testing by clicking the hyperlink in the test page which links to itself. Using the refresh button always sends the request to the server which is logic. I will try with other browsers and also maybe more than one page. – Jonathan May 13 '15 at 11:48
  • Great ! now that I just started a bounty and tried again the same code the problem just magically disappeared, what the heck :| – Jonathan May 27 '15 at 18:41
  • When you do a refresh on the browser, the usual behavior is that the whole page will be reloaded for html content. I think you our looking for IIS output caching to cache the page on the server? – Frank May 29 '15 at 22:51
  • @Frank you are right that a refresh on the browser will reload the page, but i didn't click refresh, just the link in the page – Jonathan May 30 '15 at 14:29
  • 1
    are you sure about that example response headers are belong to an asp response? I'm asking because there is a `Last-Modified` which is a static content header and couldn't be sent from asp. did you add it manually? – Kul-Tigin Jun 01 '15 at 17:41
  • @Kul-Tigin: You may have find something, indeed the presence of this header is strange, and in the version which is working now I don't have it... however I don't remember to have done anything to manually add/remove this header so I don't understand why it is there but it may be what was causing the unexpected behavior – Jonathan Jun 01 '15 at 19:39
  • If you hit F12(Developer Tools) and go to the network tab, does it show that the response should be cached? If so, what browsers have you tried testing it on? Does the response match your local machine's response? – ullfindsmit Jun 29 '20 at 17:52

1 Answers1

0

To follow up on my comment, It looks like you might be looking at caching your dynamic asp pages on the server, not on the client. Caching on the client doesnt really do you much good because modern browsers / proxies will still request the item when its an HTML document. Caching static resources which dont change such as images, css, js should work, and depending on the cache header you push out the browser will respect those.

To get your pages to cache on the server (meaning IIS doesnt have to re-generate the page) here is how you do it.

Web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <caching>
            <profiles>
                <add extension=".asp" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="00:01:00" />
            </profiles>
        </caching>
    </system.webServer>
</configuration>

You can place your web.config in a specific directory only to cache its contents, you can also break the caching using querystring params or certain request headers.

Frank
  • 1,056
  • 3
  • 17
  • 38
  • I can't easily use IIS output caching, because it is private caching (the page content vary for each user). You said: "Caching on the client doesnt really do you much good because modern browsers / proxies will still request the item when its an HTML document" Can you explain more ? Why browsers would request the server if the client-caching response headers are set properly ? – Jonathan Jun 01 '15 at 13:23
  • You can use iis-output caching and vary by param, you can also vary by different header values which you can send yoruself in your asp scripts, or you can vary by ip address. But if your pages are very different, then output caching on IIS is going to eat up resources as you will have too many versions of each page in memory> As for my previous comment, The only time I get a from Cache response is for static items (images, js, css). For content-type html chrome seems to always request the page. I dont know why, i get he same behavior on Nginx and IIS – Frank Jun 04 '15 at 19:24