Example Demo: First create a simple database, write a function read it, write a Dim variable Temp:
ASP code
The following are the referenced contents:
<%
Function Displayrecords ()
Dim SQL, Conn, RS
sql = "SELECT ID, [Szd_f], [szd_t] from admin"
Set conn = Server.CreateObject ("ADODB. Connection ")
Conn. Open "Driver={microsoft Access DRIVER (*.mdb)}; Dbq= "&server.mappath (" Db.mdb ")
Set rs = Server.CreateObject ("ADODB.") Recordset ")
Rs. Open SQL, conn, 1, 3
If not Rs. EOF Then
Dim Temp
temp = "<table width=" "90%" "align=" "Center" "
Temp = temp & "border=" "1" "bordercolor=" "Silver" ""
Temp = temp & "cellspacing=" "2" "cellpadding=" "0" ">"
Temp = temp & "<tr bgcolor=" "#CCDDEE" "><td width=" "5%" ""
Temp = temp & ">ID</td><td> operations </td>"
Temp = temp & "<td> value </td></tr>"
While not Rs. Eof
Temp = temp & "<tr><td bgcolor=" "#CCDDEE" ">"
Temp = temp & RS ("ID") & "</td><td>" & RS ("Szd_f")
Temp = temp & "</td><td>" & RS ("szd_t")
Temp = temp & "</td></tr>"
Rs. MoveNext
Wend
Temp = temp & "</table>"
Displayrecords = Temp
Else
Displayrecords = "Data not Available."
End If
Rs. Close
Conn. Close
Set rs = Nothing
Set conn = Nothing
End Function
' Write Cache
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" > read data using cache
Response.Write "... ("& Temp1 & seconds 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" > Refresh cache Display ... "
Response.Write "</font></b><br><br>"
End If
End Function
%>
<!--
Response.Write Displayrecords ()
-->
<title> read data from database---using caching </title>
<style>
Body, p, TD {Font-family:sans-serif; font-size:8pt;}
td {Padding-left:5;}
</style>
<body>
<%
Dim T1, T2
T1 = Timer
Response.Write Displaycachedrecords (20)
T2 = Timer
%>
<p align= "center" >
Stay time: <%= Left ((CDBL (T2-T1) * 1000.0), 5)%> ms
</p>
</body>
|