|
Function DisplayCachedRecords(Secs)
Dim retVal, datVal, temp1
retVal = Application("cache_demo")
datVal = Application("cache_demo_date")
If datVal = "" Then
datVal = DateAdd("s",Secs,Now)
End If
temp1 = DateDiff("s", Now, datVal)
If temp1 > 0 And retVal <> "" Then
DisplayCachedRecords = retVal
' Debugging Code :
Response.Write "<b><font color=""green"">Displaying from Cache"
Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
Response.Write "<br><br>"
Else
Dim temp2
' Change DisplayRecords() to the function whose
' value you want to cache
temp2 = DisplayRecords()
Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' Debugging Code :
Response.Write "<b><font color=""red"">Refreshing Cache ..."
Response.Write "</font></b><br><br>"
End If
End Function
Explanation
DisplayCachedRecords() takes a single argument of number of seconds after which
it will refresh the content from database. To cache data in memory it makes use of two
Application level variables; cache_demo and cache_demo_date.
In the beginning of the method it retrieves cache_demo and cache_demo_date Application
variable's values. cache_demo contains the in-memory list of players while cache_demo_date
contains the complete date till which to keep the content cached in memory, after which
it will be refreshed.
Dim retVal, datVal, temp1
retVal = Application("cache_demo")
datVal = Application("cache_demo_date")
Just in case if this is the first time this method has been called after an application
startup ( after computer reboot for example ), we add a new value in the datVal
variable making use of DateAdd() VBScript function which adds given number of seconds
in the current data.
If datVal = "" Then
datVal = DateAdd("s",Secs,Now)
End If
Now we check for the difference in number of seconds between current date ( Now() )
and the date in the datVal variable.
temp1 = DateDiff("s", Now, datVal)
If the difference is more than 0 then it means that the expiry time has not
reached, so show the content from the cache.
If temp1 > 0 And retVal <> "" Then
DisplayCachedRecords = retVal
' Debugging Code :
Response.Write "<b><font color=""green"">Displaying from Cache"
Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
Response.Write "<br><br>"
If cache time has expired then we call the DisplayRecords() method again and
retrieve the value into the cache_demo Application variable and set the new expiry
date to current date plus given number of seconds to keep the content in memory. And
then display the content to the user.
Else
Dim temp2
' Change DisplayRecords() to the function whose
' value you want to cache
temp2 = DisplayRecords()
Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' Debugging Code :
Response.Write "<b><font color=""red"">Refreshing Cache ..."
Response.Write "</font></b><br><br>"
End If
Now wasn't it simple? DisplayCachedRecords() used two Application variables, two
If ... Else statements and two VBScript functions DateAdd() and DateDiff() to do the
job for us.
This is it for DisplayCachedRecords() function. Now lets see how to call this function
from the ASP page. Open Cache.asp page and add the following code below the two functions
we already saved in it :
<html>
<head>
<title>Caching Records from Database Demo</title>
<style>
body, p, td { font-family:Sans-Serif; font-size:8pt; }
td { padding-left: 5; }
</style>
</head>
<body>
<%
Dim t1, t2
t1 = Timer
Response.Write DisplayCachedRecords(60)
t2 = Timer
%>
<p align="center">
Time Elapsed : <%= Left((CDbl((t2 - t1) * 1000.0)), 5) %> ms
</p>
</body>
</html>
Explanation
The ASP code above calls the DisplayCachedRecords() function :
Response.Write DisplayCachedRecords(60)
The only argument ( 60 ) is the number of seconds to keep the data in memory and
once this time period is reached, the content is refreshed.
Above code also makes use of Timer code to determine the number of milliseconds
consumed by the DisplayCachedRecords() function.
Summary
We built two functions using simple VBScript. One of which accesses the database every time
it is called to retrieve a list of 17 players with their first and last names. This
function was called DisplayRecords(). Then the other main function called the first
function to get a list of players and saved it in an Application variable in memory. It
also stored the expiry date in a separate variable in memory. This function was called
DisplayCachedRecords(). Every time this function was called it will display a list of
players using in-memory cache. If cache time period has expired it will refresh the data
in memory by calling DisplayRecords() again.
Tips
- To run the two functions in production environment you should remove all the Response.Write
statements under the ' Debugging Code :' comments within DisplayRecords() and
DisplayCachedRecords() functions. Writing output from within the
function is not required in production environment. We added Response.Write lines only to
help us understand how the functions were working during development.
- Also remove all the Timer code from Cache.asp page as Timer code takes unnecessary
time.
- To create your own custom cache functions, you should create two functions for
every data access function. You can use the DisplayRecords() and DisplayCachedRecords()
as templates. Change their names first. Then change the DisplayCachedRecords() functions
to use other variables than cache_demo and cache_demo_date. For example you can use
Application variables like cache_1 and cache_1_date. Then edit the DisplayRecords()
function to generate your own list of data from your own database.
Well this is it. I hope you found the expiry date limited cache technique useful.
Function DisplayCachedRecords(Secs)
Dim retVal, datVal, temp1
retVal = Application("cache_demo")
datVal = Application("cache_demo_date")
If datVal = "" Then
datVal = DateAdd("s",Secs,Now)
End If
temp1 = DateDiff("s", Now, datVal)
If temp1 > 0 And retVal <> "" Then
DisplayCachedRecords = retVal
' Debugging Code :
Response.Write "<b><font color=""green"">Displaying from Cache"
Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
Response.Write "<br><br>"
Else
Dim temp2
' Change DisplayRecords() to the function whose
' value you want to cache
temp2 = DisplayRecords()
Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' Debugging Code :
Response.Write "<b><font color=""red"">Refreshing Cache ..."
Response.Write "</font></b><br><br>"
End If
End Function
Explanation
DisplayCachedRecords() takes a single argument of number of seconds after which
it will refresh the content from database. To cache data in memory it makes use of two
Application level variables; cache_demo and cache_demo_date.
In the beginning of the method it retrieves cache_demo and cache_demo_date Application
variable's values. cache_demo contains the in-memory list of players while cache_demo_date
contains the complete date till which to keep the content cached in memory, after which
it will be refreshed.
Dim retVal, datVal, temp1
retVal = Application("cache_demo")
datVal = Application("cache_demo_date")
Just in case if this is the first time this method has been called after an application
startup ( after computer reboot for example ), we add a new value in the datVal
variable making use of DateAdd() VBScript function which adds given number of seconds
in the current data.
If datVal = "" Then
datVal = DateAdd("s",Secs,Now)
End If
Now we check for the difference in number of seconds between current date ( Now() )
and the date in the datVal variable.
temp1 = DateDiff("s", Now, datVal)
If the difference is more than 0 then it means that the expiry time has not
reached, so show the content from the cache.
If temp1 > 0 And retVal <> "" Then
DisplayCachedRecords = retVal
' Debugging Code :
Response.Write "<b><font color=""green"">Displaying from Cache"
Response.Write " ... (" & temp1 & " secs remaining)</font></b>"
Response.Write "<br><br>"
If cache time has expired then we call the DisplayRecords() method again and
retrieve the value into the cache_demo Application variable and set the new expiry
date to current date plus given number of seconds to keep the content in memory. And
then display the content to the user.
Else
Dim temp2
' Change DisplayRecords() to the function whose
' value you want to cache
temp2 = DisplayRecords()
Application.Lock
Application("cache_demo") = temp2
Application("cache_demo_date") = DateAdd("s",Secs,Now)
Application.UnLock
DisplayCachedRecords = temp2
' Debugging Code :
Response.Write "<b><font color=""red"">Refreshing Cache ..."
Response.Write "</font></b><br><br>"
End If
Now wasn't it simple? DisplayCachedRecords() used two Application variables, two
If ... Else statements and two VBScript functions DateAdd() and DateDiff() to do the
job for us.
This is it for DisplayCachedRecords() function. Now lets see how to call this function
from the ASP page. Open Cache.asp page and add the following code below the two functions
we already saved in it :
<html>
<head>
<title>Caching Records from Database Demo</title>
<style>
body, p, td { font-family:Sans-Serif; font-size:8pt; }
td { padding-left: 5; }
</style>
</head>
<body>
<%
Dim t1, t2
t1 = Timer
Response.Write DisplayCachedRecords(60)
t2 = Timer
%>
<p align="center">
Time Elapsed : <%= Left((CDbl((t2 - t1) * 1000.0)), 5) %> ms
</p>
</body>
</html>
Explanation
The ASP code above calls the DisplayCachedRecords() function :
Response.Write DisplayCachedRecords(60)
The only argument ( 60 ) is the number of seconds to keep the data in memory and
once this time period is reached, the content is refreshed.
Above code also makes use of Timer code to determine the number of milliseconds
consumed by the DisplayCachedRecords() function.
Summary
We built two functions using simple VBScript. One of which accesses the database every time
it is called to retrieve a list of 17 players with their first and last names. This
function was called DisplayRecords(). Then the other main function called the first
function to get a list of players and saved it in an Application variable in memory. It
also stored the expiry date in a separate variable in memory. This function was called
DisplayCachedRecords(). Every time this function was called it will display a list of
players using in-memory cache. If cache time period has expired it will refresh the data
in memory by calling DisplayRecords() again.
Tips
- To run the two functions in production environment you should remove all the Response.Write
statements under the ' Debugging Code :' comments within DisplayRecords() and
DisplayCachedRecords() functions. Writing output from within the
function is not required in production environment. We added Response.Write lines only to
help us understand how the functions were working during development.
- Also remove all the Timer code from Cache.asp page as Timer code takes unnecessary
time.
- To create your own custom cache functions, you should create two functions for
every data access function. You can use the DisplayRecords() and DisplayCachedRecords()
as templates. Change their names first. Then change the DisplayCachedRecords() functions
to use other variables than cache_demo and cache_demo_date. For example you can use
Application variables like cache_1 and cache_1_date. Then edit the DisplayRecords()
function to generate your own list of data from your own database.
Well this is it. I hope you found the expiry date limited cache technique useful.
|