When the data up to more than tens of thousands of, to call a few data on the page display, the ASP will slow down like a snail. Worst of all, when n users open page access, each user reads the database one at a time, which reduces efficiency, obviously, if the data can be stored in memory and then read, Certainly sped up the pace.
The so-called cache is actually in memory to open up a space to save data.
With caching you don't have to frequently access the data you keep on your hard drive, because we want every user to see the effect, consider using a Application object, because it is a common object for all visitors, stored information and defined events that can be used by the owner visitor, Here you will use the ASP built-in object application.
About application:
2 Methods [Lock and Unlock],
2 sets [content and StaticObjects],
2 events [Beginning Application_OnStart and Application_End]
Application variables will not disappear because of the user's departure, once established, until the site is closed and the program is unloaded, because of this, use the time to be particularly careful!, otherwise it will consume memory (the virtual host provider is very unhappy).
We are writing data into a custom application inside, read refreshed at the time of development, the general idea is this.
Example Demo. First create a simple database, write a function read it, write a Dim variable Temp:
Program code
<% Function displayrecords ()
' This function originally gave a variable temp to pay the value of the record
Dim SQL, Conn, RS
' Qualified SQL statements
sql = "SELECT ID, [Szd_f], [szd_t] from admin"
' Open a database connection
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
' when data conforming to SQ statement L is not displayed
If not Rs. EOF Then
' assign a temp variable to a value
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>"
'temp the assignment is finished, return it to the function
Displayrecords = Temp
Else
Displayrecords = "Data not Available."
End If
' free Memory
Rs. Close
Conn. Close
Set rs = Nothing
Set conn = Nothing
End Function%>
OK, the above function is transformed, the call time is displayrecords.
The following is application's prowess:
Program code
<%' This function is write cache
Function displaycachedrecords (secs)
Dim RetVal, Datval, Temp1
' secs is the time to refresh the data, retval is the data, Datval is the remaining time
RetVal = Application ("Cache_demo") ' get appliction value
Datval = Application ("Cache_demo_date") ' get appliction value
' judge the value of Datval, which is to calculate the time has passed
If datval = "" Then
' if it is empty, the Datval value is the current time in seconds plus the time defined by secs
Datval = DATEADD ("s", Secs,now)
End If
&APOS;TEMP1 is the second difference between the current time and the Datval.
Temp1 = DateDiff ("s", now, Datval)
' if RetVal is already the return value of the above function and the time is greater than 0
If temp1 > 0 and RetVal <> "" Then
' This function returns the number of records
Displaycachedrecords = RetVal
Response.Write "<b><font color=" "Green" > read data using cache
Response.Write "... ("& Temp1 & seconds remaining) </font></b>"
Response.Write "<br><br>"
Else
If the 'retval is empty, give the Displayrecords value to the variable Temp2
Dim Temp2
Temp2 = Displayrecords ()
' save to Application.------------------> Key
Application.Lock
Application ("Cache_demo") = Temp2
Application ("cache_demo_date") = DateAdd ("s", Secs,now)
Application.UnLock
Displaycachedrecords = Temp2
' Here, write down the past time of the cached cache, the relative total number of seconds is poor:
Response.Write "<b><font color=" "Red" > Refresh cache Display ... "
Response.Write "</font></b><br><br>"
End If
End Function
%>
Description completed.
Call method: <%=displaycachedrecords (%>)