Main Page Content
Asp Application Level Html Caching
Today, I received this email in my otherwise barren Inbox:
Contact: evolt_article@mattwarden.com
Hey I saw this post and thought "this could work on my site". Basically I have an ASP which queries an Access database.. but this wouldn't change much. How can I implement this into my page? The design will stay the same..My original post was why a certain list member shouldn't rule out ASP and CF. Chris was responding to my comments about ASP. The following was my reply to him:
Well, Chris, you'll probably find it quite trivial and redundant work, but to prepare your scripts to be able to use Application-level page caching, you should convert all Response.Write
's to variable stuffs. For instance:
Response.Write "<html><head><title>My Cool Access Query Page</title></head>" Response.Write "<body bgcolor=""#ffffff"" text=""#000000"" link=""#0000ff"">" Response.Write "<p>Hello, welcome to my cool Access query page!</p>" Response.Write "</body>" Response.Write "</html>"
Would need to become...
Dim HTML 'As String HTML = "<html>" HTML = HTML & "<head><title>My Cool Access Query Page</title></head>" HTML = HTML & "<body bgcolor=""#ffffff"" text=""#000000"" link=""#0000ff"">" HTML = HTML & "<p>Hello, welcome to my cool Access query page!</p>" HTML = HTML & "</body></html>" Response.Write HTML
These two code snipplets do EXACTLY the same thing. Note that the latter is also quicker and less resource intensive because you are only writing to the response object once, rather than multiple times. So, to set up the actual page caching, you would use this code:
<% Dim thisPageID, thisPageHTML ' find the value o thisPageID ' this will have to correspond with the naming convension you choose ' stuff the value of Application(thisPageID) into a variable ' because otherwise, you will be accessing it multiple times ' which is unnecessarily resource intensive thisPageHTML = Application(thisPageID) IF NOT thisPageHTML="" THEN Response.Write thisPageHTML ELSE HTML = "<html>" HTML = HTML & "<head><title>My Cool Access Query Page</title></head>" HTML = HTML & "<body bgcolor=""#ffffff"" text=""#000000"" link=""#0000ff"">" HTML = HTML & "<p>Hello, welcome to my cool Access query page!</p>" HTML = HTML & "</body></html>" Response.Write HTML Application(thisPageID) = HTML END IF %>
Of course, stick you access query(ies) in anywhere you want.
Now, all you have to do is force a reconstruction of the page whenever the database is modified.
I've found that the best way to do this is to create a function/sub calledResetVars()
or whatever in an include file and include it on all pages. This file will hold all the page caching variables and code for setting them to "". Then, when you update the database, just callResetVars()
. Make sure that you keep this file updated, though.
Well, say that you need to useResponse.Flush
some time to let the user know that the page hasn't just hung up. Just change the HTMLvar
segment to this:
Dim HTML 'As String HTML = "<html>" HTML = HTML & "<head><title>My Cool Access Query Page</title></head>" HTML = HTML & "<body bgcolor=""#ffffff"" text=""#000000"" link=""#0000ff"">" Response.Write HTML Response.Flush HTML2 = HTML2 & "<p>Hello, welcome to my cool Access query page!</p>" HTML2 = HTML2 & "</body></html>" Response.Write HTML2 Application(thisPageID) = HTML & HTML2
Get the idea? Hope this has cleared things up. Let me know if it hasn't.Now, what I neglected to mention to Chris is that you can also cache certain things that will almost never change. How many of you have an ASP script which populates a select box with state names? Hmmmm... how often have you had to update the database for this state box? Well, then. Store it in an application-level variable. Best thing is that this can be used for all ASP pages (in that Application scope) that need it. Think about it
Contact: evolt_article@mattwarden.com