Most dynamic websites will usually update their content based on a few items. The
first most and obvious occurrence would be that the content of the page actually
changed; Either your XML file has been re-written or your database contains updated
information. The second occurrence would be parameters passed either by the client
or the server to the page or web application.
Content on dynamic web pages is rarely real time, unless you are in an interactive
page such a forum. Most of the time content will not update in real time. Users
browsing a web site will consistently view the same information. So if
user A
loads the front page of a web site, and
user B loads the same
front page 20 seconds later they will see the exact same content. before asp.net Microsoft really did not have a real solution to this problem. In ASP you can use
application variables but this gets extremely cumbersome and buggy; I do not recommend
going this route. If you have to solve this problem in an asp environment the best
solution I have found is to write small .exe programs in VB6 or VC++ that use the
File.System.Object to write out static html files or .asp files that dont hit the
database. You can schedule the .exe files to run in
scheduled tasks to run
at regular intervals.
ASP.net offers a solution to our problem,
output caching.
By just implementing a simple tag the entire content of your page gets cached as html
to main memory on your web server. So when
user B loads your front page not only
does that user not force the server to re parse your XML and re query your database, but
that request comes straight from main memory, the hard drive does not get hit at all
! This works great and your web page will scream even if you syndicate content from outside
resources or complex database types. To implement
output caching simply drop this
line of code anywhere in your asp.net file:
<%@ OutputCache Duration = "1200" VaryByParam="none" %>
Ok well thats all great but in reality you really need to cache a few different
versions of your front page. Your data may not update in less than 1200 seconds. ( 20
minutes ) but the page may need to refresh if a
page level or
application
level parameter changes. There are a few ways of dealing with this one would be
partial page caching, you can cache only web controls by simply putting the output
cache
tag within the web control. Personally I do not like this solution, I am looking to send
an instantaneous response to the user. And with 1 gig of memory on my server its got
plenty of space to store all those 30-50k cached documents.
page level parameters
Page level parameters such as querystring or form variables are parameters that are
passed to a page. To cache multiple versions of a page based on page level parameters
you can use
VaryByParam.
You do not want:
http://www.myserver.com/article.aspx?articleid=17
to output the same exact information as
http://www.myserver.com/article.aspx?articleid=21
So in this case you would specify:
<%@ OutputCache Duration = "1200" VaryByParam="articleid" %>
This would now cache a separate version of the page for each articleid. To list
multiple parameters simply separate them with a semicolon or use a * (wild card) to create
a separate cach for any page level.
<%@ OutputCache Duration = "1200" VaryByParam="*" %>
application level parameters
Application level parameters such as session variables or client side cookies are parameters
that are page independent. To cach multiple versions of a page based on application
level parameters you can use VarByCustom and the
global.asax file.
In my case the problem I was faced with was using Forms.Authentication in combination
with output caching. If user A logged into my site I did not want it to "Welcome User
A" to User B.
he first thing we will need to do is set the location of the output cache to "client
or "server, this ensures that the page will not be cached by a proxy server. To learn
more about caching location please visit:
This Link
Secondly you will need to set up an override function in your global.asax file
<Script language="VB" runat="server">
Public Overrides Function GetVaryByCustomString( _
ByVal context As System.Web.HttpContext, _
ByVal custom As String) As String
Select custom
Case "username"
Return Context.User.Identity.Name
Case Else
Return MyBase.GetVaryByCustomString(context, custom)
End Select
End Function
</script>
And lastly specify your custom variable:
<%@ OutputCache Duration = "1200" VaryByParam="*" Location="Server" VaryByCustom="username"
%>
So now if a user logs in he will receive his own custom cached page, as he browses
around the site and hits the same pages, he will receive the pages that were cached
for him.