Server-side content caching in classic ASP
To minimize load on a backend SQL server and/or to ensure snappy performance on a website serving dynamic content, caching whole pages or parts of pages is often required.
This document gives a quick example on how to manage server-side content caching in the application object in classic ASP. Note that if you are building your pages in ASP.NET, there is a build-in caching system in the framework. However in classic ASP, you have to do it yourself, and this is one way of implementing a cache system.
The implementation is heavily inspired by 4 Guys From Rolla's A Real-World Example of Caching Data in the Application Object article.
Global.asa:
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("UseCaching") = True
Application("X_CacheAge") = 15 ' minutes
Application("X_DateCached") = DateAdd("d", -1, Now())
End Sub
Sub Application_OnEnd
' Your application shutdown stuff here
End Sub
Sub Session_OnStart
' Your session start stuff here
End Sub
Sub Session_OnEnd
' Your session end stuff here
End Sub
</script>
Sub Application_OnStart
Application("UseCaching") = True
Application("X_CacheAge") = 15 ' minutes
Application("X_DateCached") = DateAdd("d", -1, Now())
End Sub
Sub Application_OnEnd
' Your application shutdown stuff here
End Sub
Sub Session_OnStart
' Your session start stuff here
End Sub
Sub Session_OnEnd
' Your session end stuff here
End Sub
</script>
Use the following code in the page needing cached content. The final content (coming from either the cache or the database depending on the cache age) will be stored in the strContent variable. The GetContentFromDB() function should return the content needing caching. Note that the application object is not limited to storing text strings. An array can for example be used to store multiple elements needed to generate the final page.
Dim strContent
If Application("UseCaching") = True Then
If (DateDiff("n", Application("X_DateCached"), Now()) > Application("X_CacheAge")) Or (IsEmpty(Application("cache_X"))) Then
strContent = GetContentFromDB()
Application.Lock
Application("cache_X") = strContent
Application("X_DateCached") = Now()
Application.UnLock
Else
strContent = Application("cache_X")
End If
Else
strContent = GetContentFromDB()
End If
If Application("UseCaching") = True Then
If (DateDiff("n", Application("X_DateCached"), Now()) > Application("X_CacheAge")) Or (IsEmpty(Application("cache_X"))) Then
strContent = GetContentFromDB()
Application.Lock
Application("cache_X") = strContent
Application("X_DateCached") = Now()
Application.UnLock
Else
strContent = Application("cache_X")
End If
Else
strContent = GetContentFromDB()
End If
Note that the cache entry is named "cache_X". By using this naming convention (prefixing different cache elements with "cache_"), a central cache-clearing function can be implemented:
Sub ClearCache()
Dim objAppKey
Application.Lock
For Each objAppKey in Application.Contents
If Left(objAppKey, 6) = "cache_" Then
Application(objAppKey) = Empty
End If
Next
Application.UnLock
End Sub
Dim objAppKey
Application.Lock
For Each objAppKey in Application.Contents
If Left(objAppKey, 6) = "cache_" Then
Application(objAppKey) = Empty
End If
Next
Application.UnLock
End Sub
Tags: asp
Page last updated 2007-07-15 17:50. Some rights reserved (CC by 3.0)