The so-called cache is actually in memory to open a space to save data, using caching you do not have to frequently access the data you keep on your hard disk, because we want every user to see the effect, consider using a Application object, because it is a shared object for all visitors, stored information and defined events can be used for the owner visitor, here to use the ASP built-in object application, there are 2 methods for application [Lock and unlock],2 collections [content and StaticObjects], 2 events [Starting 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 occupy the memory, I do not need to say here, interested in the access to relevant information, is generally the case. We are writing data into a custom application inside, read refreshed at the time of the design, the general idea is this.
Example Demo. First create a simple database, write a function read it, write a Dim variable Temp:
The following is a reference fragment:
Copy Code code as follows:
Function Displayrecords ()
' This function originally gave a variable temp to pay the value of the record
Dim SQL, Conn, RS
' Eligible 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 that conforms to SQ statement L is not displayed
If not Rs. EOF Then
' Assign a value to a temp variable
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 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:
Copy Code code as follows:
' 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 rest of the time
RetVal = Application ("Cache_demo") ' Gets the value of appliction
Datval = Application ("Cache_demo_date") ' Gets the value of appliction
' To 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
' 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
' RetVal is empty, give displayrecords value to the variable Temp2
Dim Temp2
Temp2 = Displayrecords ()
' Save to Application.------------------> Focus
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.
The following is a complete no comment code
Call method: <%=displaycachedrecords (%>)
Write in the back: if you feel that your server memory is not large enough, do not use the cache heavily.