Simple caching system
Simple caching system
By Roland Mensenkamp
15 June 2001
This article shows how you can create a caching system with the use of a
Application variables. Advantage of such a system is that your code will be much
faster because you don't have to go to the database on every request to retrieve the data.
This can improve the performance of a site 1000 x.
The function and sub-routine are really simple and flexible. You can easily use them
over the entire site, simply by adding them to the file.
The function 'Check' checks if there's data in the Application variable and if it's up-to-date.
It has 3 parameters:
T_name = Application Timer name
A_name = Application name
T_compare = How long the application shouldn't update the cache (seconds)
The sub 'Save' is used to save the values in an Application variable.
It also has 3 parameters:
T_name = Application Timer name (name you want to give it)
A_name = Application name (name you want to give it)
N_value = New value that you want to save in the Application variable
Remark: Don't save objects in Session or Application variables!
Certain kinds of objects give big problems
(see Session overview & myths)
cachefunc.asp
<%
Function Check(T_name, A_name, T_compare)
If Application (A_name) = ""_
Or DateDiff("n", Application(T_name), Now()) > T_compare Then
Check = True
Else
Check = False
End If
End Function
Sub Save(T_name, A_name, N_value)
Application.Lock
Application(T_name) = Now()
Application(A_name) = N_value
Application.Unlock
End Sub
%>
page.asp
<!--#include file="Cachefunc.asp"-->
<%
If Check("T1", "A1", 1) Then
sNvalue = "With your new value"
Call Save("T1", "A1", sNvalue)
Else
Response.Write Application("A1")
End If
%>
As you can see it's fairly simple, and really worth while, if performance is
important to you. It only works of course when the data doesn't change constantly
and it doesn't vary per user. If the users don't have to see changes immediately,
then this is very handy.
|