Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Preventing Cache 6

Status
Not open for further replies.

ToddWW

Programmer
Mar 25, 2001
1,073
US
In most cases, with ASP applications, browser cache is our enemy.

ASP pages are, by nature, dynamic. In contrast, HTML pages are static. A great deal of work in the Web arena has gone into improving the response time for HTML pages. Cache was designed to help solve this problem.

Page caching works beautifully for static HTML pages, but doesn't work well at all for ASP pages. ASP pages often change because the back-end content has changed or the content changes in response to user input. If the browser displays a cached page, the page may be out of date, or may not respond properly to user input. The ASP server-side code does not run because the proxy intercepts the request before it reaches your server. The Response object has several properties that can help solve this problem. However, there are some problems with these that you should be aware of.


Response.Expires and Response.ExpiresAbsolute
Two good methods with easy properties to use and understand. However, they don't work all the time. The problem doesn't lie within the commands themselves--they do add the proper header values to the response; it's that not all browsers act the way you might expect. Netscape browsers are particularly guilty of this improper behavior. Futhermore, even if a browser is honoring the content expiration, a proxy server within a corporate network may intercept the request and return a cached page from the proxy server. Proxy servers don't always honor the expiration date either. To force the proxy to request a fresh copy of the page, you can use the Response.CacheControl property.


Response.CacheControl
Set this to private to tell the proxy server NOT to cache the page. Most proxy servers will not cache pages if the CacheControl value is set to private.


Again, unfortunately not all proxy servers honor the CacheControl header either. With all of the uncertainty around who will and who will not honor response headers in web documents, ASP programmers have evolved a sure-fire method to force ASP page requests to refresh from the application server rather than from the cache or proxy server. That method involves changing the URL in some unimportant way for each request.

Here is what I do at the top of every ASP page in my application.


<%@ Language=VBScript %>
<% Option Explicit %>
<%
dim uUrl
uUrl = &quot;uUrl=&quot; & Date & Timer
%>


Then anywhere in my document where I am navigating to another page, I use that variable in the URL. Here are some examples of that.



<%
Response.Redirect &quot;somepage.asp?ID=1422&&quot; & uUrl
%>


<form action=&quot;somepage.asp?<%=uUrl%>&quot;>


<a href=&quot;somepage.asp?ID=1422&Search=find+me&<%=uUrl%>&quot;>


Because the date and time change constantly, the above method will append a unique string to the end of every URL reference in your application. Thus, the browser will be unable to locate it in cache.

The content in the FAQ is based on personal experience as well as many ASP references in my library. I use all of the methods above and have never had a single problem with caching in my ASP application.

Happy Caching :)

ToddWW
 
ToddWW, I have a FAQ posted over in the FAQ section of this forum about this. I had included the four lines that have been posted in this forum several times, but it does not include your method there, which I hadn't thought of...

Would you mind my adding this to the FAQ (with your name on it, of course) to make it more complete?

lemme know -
paul
penny.gif
penny.gif
 
I don't mind at all.

ToddWW :)
 
Excellent. Thanks! :)

faq333-1034

If you have any more additions/modifications, or whatever, just post back to this thread and I'll make them.

paul
penny.gif
penny.gif
 
This is a great ideea...ToddWW
I use an method witch looks like this but more complex...
I dont even think to use the Date Time function for that...
I use Randomize and Rnd functions and lots of other things...
Any way thanks for your ideea... ________
George, M
 
why can't you just include this one line in the head -->

<META HTTP-EQUIV=&quot;Pragma&quot; CONTENT=&quot;no-cache&quot;>
 
You can, and I would encourage everyone to do that. However, not all proxy servers and / or browsers honor ALL of the different headers that can be sent to the browser to prevent cache, as explained in the original post of this FAQ.

ASP has many methods to send those headers automatically in the Response Object. But many advanced ASP developers, including authors to most of the top ASP references, will argue that those methods do not provide a Sure Fire solution to preventing cache in ALL browsers.

I've seen numerous posts in this forum alone from developers who have used every method possible, by sending headers to browers, to prevent cache. And yet, they still incurred problems with some browsers or networks with proxy servers.

Hope this sheds some additional light on the subject.

ToddWW :)
 
I'm not using VBScript...would this still work with Javascript??
 
Yes, the great thing about the solution here is that all you are doing is passing a unique value in the address so that the intervening proxies or gateways are forced to load the page from scratch. This can be done not only in ASP in javascript but also any other language such as JSP, Perl, Cold Fusion, etc...
In this case it's the technique that solves the problem, not necessarally the exact code. The same technique could be written in a differant manner in VBscript and would still work as long as it was providing that unique look that would cause the gateways/proxies/etc to ignore the cached copy and request a new one.
-Tarwn &quot;The problem with a kludge is eventually you're going to have to back and do it right.&quot; - Programmers Saying (The Wiz Biz - Rick Cook)
&quot;Your a geek!&quot; - My Girlfriends saying
 
These beauties only come around once in a while.

Brilliant idea.

Star for that one ToddWW
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top