Library code snippets
Preventing Caching
A common problem when creating ASP pages is that although the data usually comes 'live' from the database, the browser often caches that information. Although this can help to reduce the bandwidth usage for your site, it also means that if the user has loaded the page before, he/she could be viewing out of date or inaccurate information.
There are a number of ways around this. First, you can set
Response.Expires = -1
which means that the page will always expire, and the browser will load a new copy. Alternatively, you can set a date on which the page will expire:
Response.ExpiresAbsolute = "5 December 2001"
or, simply the number of seconds in the future:
Response.Expires = 1000 'expires after 1000 seconds
However, in situations such as a shopping cart, even this is not enough. You will find that if the user presses the back button on their browser, more often that not, it doesn't bother re-loading the page... which can cause problems if the user has just completed the order, and then goes back to a full shopping cart. To get around this, there is one last trick... add this code:
'date in the past...
Response.AddHeader "Expires", "Mon, 26 Jul 1997 05:00:00 GMT"
'always modified
Response.AddHeader "Last-Modified", Now & " GMT"
'HTTP/1.1
Response.AddHeader "Cache-Control", "no-cache, must-revalidate"
'HTTP/1.0
Response.AddHeader "Pragma", "no-cache"
'last ditch attempt!
Response.Expires = -1
This code adds a number of fields to the header information sent to the browser, and seems to be effective whatever the browser!
Related articles
Related discussion
-
Header and Footer in Web page print
by fhajaj (4 replies)
-
help me to get simple requirement
by Slicksim (1 replies)
-
Gridview -> Template Field -> Button
by antti.simonen (1 replies)
-
Classic ASP : Page expires
by chezhian_in05 (0 replies)
-
ASP VS PHP
by paulfp (9 replies)
Related podcasts
-
ASP.NET Caching and Performance
Steve Smith, owner of ASP Alliance and Lake Quincy Media joins us today to teach us about some hidden gems in ASP.NET caching and performance. Steve’s expertise in this area comes from first-hand experience as Lake Quincy’s ad system serves over 60 requests per second and handles over 150 million...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
Technical Support Engineer EMEA
in Reading (£50K-£50K per annum) -
Solutions Engineer
in Reading (£50K-£60K per annum)
... as the cache resides on the client, it is entirely up to the users browser what it does - fortunately the server can't tell a client "now delete files xyz from your hard disk" - it doesn't even have to obey the commands we've sent it telling it not to cache the individual pages.
Although this code deletes cache for an individual page, I was wondering if there was a way to delete all page cache when a user logs out. Does anyone have any ideas on how this can be done instead of having each individual page expire immediately???
This thread is for discussions of Preventing Caching.